Dim MyRoleStore As New RoleStore(Of IdentityRole) Dim MyRoleManager As New RoleManager(Of IdentityRole)(MyRoleStore) MyRoleManager.Create(New IdentityRole("Support Staff"))
IdentityRole is the Entity Framework object that ultimately represents (mapped to) a row in the AspNetRoles table.
Once you have some roles defined, you can associate a role to a user like this:
Dim Result As IdentityResult = _ Await UserManager.AddToRoleAsync(MyUser.Id, "Support Staff")
Finally in order to restrict parts of your application, you simple add an Authorize filter to the ActionMethod you want restricted like this:
<Authorize(Roles:="Support Staff")> Function MyActionMethod() As ActionResult ... Return View() End Function
Finally, if the user is not authorized, they will be redirected to a specific page, usually the login page. ASP.NET Identity is not forms authentication so this redirect is not in the web.config. In an MVC app, it is in the Startup.Auth class ConfigureAuth method:
app.UseCookieAuthentication(New CookieAuthenticationOptions() With { .AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, .LoginPath = New PathString("/Account/Login")})
No comments:
Post a Comment