我有一个带有ASP.NET标识(个人帐户)的ASP.NET MVC 5 Web应用程序。但是我需要能够从控制台应用程序注册新用户。
因此,我将一些ASP.NET Identity类从Web应用程序移到类库中,以在Web应用程序和CLI之间共享。
我已经成功移动了以下内容:
public class PortalDbContext : IdentityDbContext<PortalUser>
{
public PortalDbContext(string connectionString)
: base(connectionString, throwIfV1Schema: false)
{
}
public static PortalDbContext Create(string connectionString)
{
return new PortalDbContext(connectionString);
}
}
public class PortalUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<PortalUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class PortalUserManager : UserManager<PortalUser>
{
public PortalUserManager(IUserStore<PortalUser> store) : base(store)
{
}
public async Task<IdentityResult> RegisterUser(string email, string password)
{
PortalUser user = new PortalUser { UserName = email, Email = email };
return await this.CreateAsync(user, password);
}
}
但我不知道从哪里得到IUserStore<PortalUser>
的PortalUserManager
,从需求。
在Web应用程序中,检索到HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>()
了我显然不能在类库中使用的该管理器。
我已经结束了加入一个静态方法Create(string connectionString)
来PortalUserManager
将创建UserStore
并DbContext
返回经理的新实例。
public class PortalDbContext : IdentityDbContext<PortalUser>
{
public PortalDbContext(string connectionString)
: base(connectionString, throwIfV1Schema: false)
{
}
public static PortalDbContext Create(string connectionString)
{
return new PortalDbContext(connectionString);
}
}
public class PortalUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<PortalUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class PortalUserManager : UserManager<PortalUser>
{
public PortalUserManager(IUserStore<PortalUser> store) : base(store)
{
}
public static PortalUserManager Create(string connectionString)
{
UserStore<PortalUser> userStore = new UserStore<PortalUser>(PortalDbContext.Create(connectionString));
PortalUserManager manager = new PortalUserManager(userStore);
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<PortalUser>(manager)
{
AllowOnlyAlphanumericUserNames = true,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
return manager;
}
public async Task<IdentityResult> RegisterUser(string email, string password)
{
PortalUser user = new PortalUser { UserName = email, Email = email };
return await this.CreateAsync(user, password);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句