Asp.net mvc 5 MVC5(VS2012)用户管理器不';t登录用户
这是本书的延续 如果我覆盖userManager:Asp.net mvc 5 MVC5(VS2012)用户管理器不';t登录用户,asp.net-mvc-5,claims-based-identity,asp.net-identity,owin,Asp.net Mvc 5,Claims Based Identity,Asp.net Identity,Owin,这是本书的延续 如果我覆盖userManager: public class NHibernateAspnetUserManager<TUser> : UserManager<TUser> where TUser : IdentityUser { public NHibernateAspnetUserManager(IUserStore<TUser> store) : base(store) { } public overri
public class NHibernateAspnetUserManager<TUser> : UserManager<TUser> where TUser : IdentityUser
{
public NHibernateAspnetUserManager(IUserStore<TUser> store) : base(store)
{
}
public override Task<ClaimsIdentity> CreateIdentityAsync(TUser user, string authenticationType)
{
var identity = new ClaimsIdentity();
identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
return Task.FromResult(identity);
}
}
编辑
找出发生错误的原因。用户对象返回时没有ID,因此默认的UserManager感到不安。修复了这个问题,并使用了默认的UserManager,它现在不再抛出错误,但仍然不会让用户登录。据我所知,它返回的标识对象看起来不错
进一步说明
所以我安装了VS2013,并复制了商店和NHibernate回购协议,所有这些都是第一次工作。我只能假设,在VS2012中创建和更新MVC5与在VS2013中执行MVC5之间存在一些可弥补的差异。因此,主要问题是,您不尊重方法中的身份验证类型,您需要为DefaultAuthenticationType.ApplicationOkie创建一个ClaimsEntity,默认声明工厂会这样做:
public override Task<ClaimsIdentity> CreateIdentityAsync(TUser user, string authenticationType)
{
var id = new ClaimsIdentity(authenticationType, UserNameClaimType, RoleClaimType);
id.AddClaim(new Claim(UserIdClaimType, ConvertIdToString(user.Id), ClaimValueTypes.String));
id.AddClaim(new Claim(UserNameClaimType, user.UserName, ClaimValueTypes.String));
public override Task CreateIdentityAsync(用户,字符串身份验证类型)
{
var id=新的ClaimsEntity(authenticationType、UserNameClaimType、RoleClaimType);
id.AddClaim(新声明(UserIdClaimType、ConvertIdToString(user.id)、ClaimValueTypes.String));
id.AddClaim(新声明(UserNameClaimType,user.UserName,ClaimValueTypes.String));
我在使用ASP.NET 4.5实现自定义标识时遇到了同样的问题。问题实际上是在索赔集合中添加空值(请参见注释):
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务寄存器(RegisterViewModel模型)
{
if(ModelState.IsValid)
{
var user=new-AppUser{UserName=model.UserName};
//UserManager创建用户后,所有
//自动丢弃除“用户名”之外的AppUser
var result=await UserManager.CreateAsync(新AppUser
{
UserRealName=model.UserRealName,
用户名=model.UserName,
Password=model.Password
},型号。密码);
if(result.successed)
{
//所以我们需要重新获得新用户
user=AppUser.GetByName(model.UserName);
wait SignInAsync(user,false);//否则我们将在这里添加空值。。。
返回重定向到操作(“索引”、“主页”);
}
加法器(结果);
}
返回视图(模型);
}
专用异步任务符号同步(AppUser用户,布尔值isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationOkie);
var identity=await UserManager.CreateIdentityAsync(用户,
DefaultAuthenticationTypes.ApplicationOkie);
AuthenticationManager.SignIn(将新的AuthenticationProperties/…添加到
//声明除用户名外的所有AppUser属性
{ispersist=ispersist},标识);
}
public override Task<ClaimsIdentity> CreateIdentityAsync(TUser user, string authenticationType)
{
var id = new ClaimsIdentity(authenticationType, UserNameClaimType, RoleClaimType);
id.AddClaim(new Claim(UserIdClaimType, ConvertIdToString(user.Id), ClaimValueTypes.String));
id.AddClaim(new Claim(UserNameClaimType, user.UserName, ClaimValueTypes.String));
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new AppUser { UserName = model.UserName };
// after the UserManager creates a user, all the properties of
// AppUser except "UserName" are automatically discarded
var result = await UserManager.CreateAsync(new AppUser
{
UserRealName = model.UserRealName,
UserName = model.UserName,
Password = model.Password
}, model.Password);
if (result.Succeeded)
{
// So we need to re-get the new user
user = AppUser.GetByName(model.UserName);
await SignInAsync(user, false); // otherwise here we will add null values ...
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
return View(model);
}
private async Task SignInAsync(AppUser user, Boolean isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
var identity = await UserManager.CreateIdentityAsync(user,
DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties // ... into the list of
// claims for all AppUser properties except UserName
{ IsPersistent = isPersistent }, identity);
}