我正在尝试使用asp.net身份实施基于角色的身份验证,并浏览了许多站点和教程,我想我已经快到了,但是下面缺少一些内容。
仅供参考,我更改了身份代码,以便所有主键均为整数类型而不是字符串。
话虽如此,在一些教程和示例(即https://code.msdn.microsoft.com/ASPNET-MVC-5-Security-And-44cbdb97)之后,我在Startup.vb类中添加了以下代码,以创建管理员和用户角色:
Imports Microsoft.AspNet.Identity
Imports Microsoft.AspNet.Identity.EntityFramework
Imports Microsoft.Owin
Imports Owin
Imports System.Security.Claims
<Assembly: OwinStartupAttribute(GetType(Startup))>
Partial Public Class Startup
Public Sub Configuration(app As IAppBuilder)
ConfigureAuth(app)
CreateRolesandUsers()
End Sub
' https://code.msdn.microsoft.com/ASPNET-MVC-5-Security-And-44cbdb97
Private Sub CreateRolesandUsers()
Dim context As ApplicationDbContext = New ApplicationDbContext()
Dim roleManager = New RoleManager(Of IdentityRole)(New RoleStore(Of IdentityRole)(context))
Dim UserManager = New UserManager(Of ApplicationUser, Integer)(New UserStore(Of ApplicationUser)(context))
If Not roleManager.RoleExists("Admin") Then
Dim role = New Microsoft.AspNet.Identity.EntityFramework.IdentityRole()
role.Name = "Admin"
roleManager.Create(role)
Dim user = New ApplicationUser()
user.UserName = "MyAdmin"
user.Email = "myemail@gmail.com"
Dim userPWD As String = "Testing@DRFFG"
Dim chkUser = UserManager.Create(user, userPWD)
If chkUser.Succeeded Then
Dim result1 = UserManager.AddToRole(user.Id, "Admin")
End If
End If
If Not roleManager.RoleExists("User") Then
Dim role = New Microsoft.AspNet.Identity.EntityFramework.IdentityRole()
role.Name = "User"
roleManager.Create(role)
End If
End Sub
End Class
以下代码行引发了主题错误,特别是在我突出显示的部分中:
Dim UserManager = New UserManager(Of ApplicationUser, Integer)(New UserStore(Of ApplicationUser)(context))
关于什么可能是错误的任何想法?
谢谢