In an ASP.NET application, what is the way to in the view to check if user authenticated and roles?
I mean assuming you are using Asp.NET Identity resources and you have already scaffolded identity. Is it through HTTPContext? I know how to do it with [Authorize] in the controller, but that would require creating separate views for authenticated and non-authenticated viewers right? Is there an easier way through something like:
5 Replies
Inject
SignInManager
into the view and check SignInManager.IsSignedIn(User)
I think User.Identity is { IsAuthenticated: true }
would work as well
No need to inject anything in this caseOk, what about to check roles directly in the view?
User.IsInRole("The Role")
Is user a var that is automatically created by the system and can be accessed?
Yep, it's a property of the view, basically