Reputation: 51
I am working with Visual Studio 2019 (.NET 4.7). My project is an ASP.NET MVC web app, without Microsoft Identity.
I try to implement the mechanism of authentication and authorization because I want to use the attributes like [Authorize(Roles = "admins")]
on my controllers/actions.
I don't want to use Microsoft Identity, EF and OWIN.
I started to implement form authentication, and I can get everywhere in my code the current user name with User.Identity.Name
which has the same value as FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe)
used in the login process.
I can store the roles in the field UserData
inside the FormsAuthentication
ticket.
Now, I want to implement the roles in order to activate the [Authorize]
attributes and to use the function User.IsInRole("***")
everywhere in my code.
Can someone help me ?
Upvotes: 0
Views: 39
Reputation: 51
Maybe this topic is too old. That is why nobody answered.
Anyways, I found the solution by reading this post Set custom IIdentity or IPrincipal.
In short, customize Iprincipal (I included the role in that classe and implemented the function IsInRole) and implement the event handler Application_PostAuthenticateRequest to calculate for each request the customized object IPrincipal.
Upvotes: 0