C
C#11mo 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
Angius11mo 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
GipperOP11mo ago
Ok, what about to check roles directly in the view?
Angius
Angius11mo ago
User.IsInRole("The Role")
Gipper
GipperOP11mo ago
Is user a var that is automatically created by the system and can be accessed?
Angius
Angius11mo ago
Yep, it's a property of the view, basically
Want results from more Discord servers?
Add your server