MVC5(VS2012)UserManager未对用户进行签名

时间:2014-02-21 10:04:10

标签: asp.net-mvc-5 claims-based-identity asp.net-identity owin

这是this question的延续。

如果我覆盖userManager:

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);
    }
}

这不会抛出任何错误但不会记录用户,(日志过程发生但@Request.IsAuthenticated将始终返回false)。如果我不覆盖它,那么我得到一个“System.Security.Claims.Claim..ctor”错误,如另一个问题中所述。尝试解决我自己的用户实现IUserClaimStore但只返回一个新的声明列表。

我不确定默认的usermanager在底层有什么不同。我猜它设置了某种形式的声明身份对象,允许MVC识别某人已登录。

var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);            
AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent}, identity); 

修改

找出ctor错误发生的原因。用户对象在没有ID的情况下返回,因此默认的UserManager变得不安。修复了这个并使用了默认的UserManager,它现在不再抛出错误,但仍然没有记录用户。它返回的身份对象看起来很好。我可以告诉你。

进一步说明

所以我安装了VS2013并复制了商店和NHibernate repo,这些都是第一次工作。我只能假设在创建它和在VS2012中更新MVC5并在VS2013中进行更新之间存在一些差异。

2 个答案:

答案 0 :(得分:0)

所以主要问题是您不尊重方法中的身份验证类型,您需要为DefaultAuthenticationType.ApplicationCookie创建一个ClaimsIdentity,这是默认声明工厂所做的:

    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));

答案 1 :(得分:0)

我遇到了使用ASP.NET 4.5实现自定义标识的相同问题。问题实际上是在声明集合中添加空值(请参阅注释):

[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);
}
相关问题