使用VS 2015,我构建了一个Web应用程序,并将其放入ASP身份模型。这是我第一次使用此代码,因此可以理解,我对此并不精通。
我正在使用C#ASP.Net Web窗体。
我在修改UserManager的令牌和验证时遇到问题。我已经看到了一些使用IdentityConfig.cs的示例,但是最终我所有的代码都在IdentityModels.cs中。我已经设法更新了IdentityModel的整数用户ID和其他内容,但是现在修改usermanager似乎超出了我的范围。
这是IdentityModels.cs中的相关代码:
public class UserManager : UserManager<ApplicationUser, int>
{
public UserManager(): base(new UserStore<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>(new ApplicationDbContext()))
{
}
public static UserManager<ApplicationUser, int> Create(IdentityFactoryOptions<UserManager> options, IOwinContext context)
{
var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ApplicationName");
var userManager = new UserManager<ApplicationUser, int>(new CustomUserStore(context.Get<ApplicationDbContext>()));
userManager.UserValidator = new UserValidator<ApplicationUser, int>(userManager)
{
AllowOnlyAlphanumericUserNames = false
};
userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser, int>(provider.Create("ApplicationToken"));
return userManager;
}
}
这似乎根本没有注册。我从来没有设法使令牌部分正常工作,最终我将UserValidator硬编码在用户创建页面中。
如我所料,它可以正常工作
var manager = new UserManager();
var user = new ApplicationUser()
{
UserName = nuUserName.Text,
Email = nuUserName.Text,
PhoneNumber = nuPhoneNumber.Text,
FirstName = nuFirstName.Text,
LastName = nuLastName.Text
};
manager.UserValidator = new UserValidator<ApplicationUser, int>(manager)
{
AllowOnlyAlphanumericUserNames = false
};
IdentityResult result = manager.Create(user, nuPassword.Text);
我实际上也曾尝试将其添加到IdentityConfig.cs文件中,但是仍然似乎没有发现这段代码。这就是我现在在该文件中所拥有的:
public class ApplicationUserManager : UserManager<ApplicationUser, int>
{
public ApplicationUserManager(IUserStore<ApplicationUser, int> store) : base(store) { }
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new CustomUserStore(context.Get<ApplicationDbContext>()));
manager.UserValidator = new UserValidator<ApplicationUser, int>(manager)
{
AllowOnlyAlphanumericUserNames = false
};
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser, int>(dataProtectionProvider.Create("ApplicationToken"));
}
return manager;
}
}
我确定所缺少的很简单,只是看不到。