C
C#5mo ago
Gipper

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:
@if(check_if_authenticated)
{
//do stuff
}
@if(check_if_authenticated)
{
//do stuff
}
5 Replies
Angius
Angius5mo ago
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 case
Gipper
Gipper5mo ago
Ok, what about to check roles directly in the view?
Angius
Angius5mo ago
User.IsInRole("The Role")
Gipper
Gipper5mo ago
Is user a var that is automatically created by the system and can be accessed?
Angius
Angius5mo ago
Yep, it's a property of the view, basically
Want results from more Discord servers?
Add your server
More Posts