MVC5 (VS2012) 标识 CreateIdentityAsync - 值不能为空 [英] MVC5 (VS2012) Identity CreateIdentityAsync - Value cannot be null

查看:18
本文介绍了MVC5 (VS2012) 标识 CreateIdentityAsync - 值不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 MVC5 站点(在 VS2012 中)设置 OAuth.

I am trying to setup OAuth for a an MVC5 site (in VS2012).

我正在使用 Fluent NHibernate.我已经设置了我自己的用户存储并传入一个存储库对象来访问 NHibernate 会话对象.我将我的商店传递给默认的 aspnet 用户管理器提供程序.这最终适用于本地注册和登录.我不是在尝试设置与 Facebook 的连接/注册.

I am using Fluent NHibernate. I have setup my own Userstore and pass in a repository object to access NHibernate session object. I pass my store into the default aspnet usermanager provider. This eventually worked for local registration and logging in. I am not trying to setup connecting / registering with Facebook.

它获得了一个成功的帐户.在user表中添加一个用户,在logins表中添加一条记录然后炸掉.我没有在用户存储中实现声明,也没有在用户对象中放置声明集合.(不确定这是否真的需要,我正在剥离所有可能出错的东西以找到问题的根源).

It gets a successful account. Adds a user in the user table, adds a record in the logins table and then blows up. I have not implements claims in the user store, or put a claims collection in the user object. (not sure if this is actually required, I was stripping everything back that might be going wrong to find the source of the issue).

爆炸的行是,(在帐户控制器中):

The line that blows up is, (in the account controller):

var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);            

在这个方法中:

private async Task SignInAsync(IdentityUser user, bool isPersistent)

这是堆栈跟踪的结尾

[ArgumentNullException: Value cannot be null.
Parameter name: value]
   System.Security.Claims.Claim..ctor(String type, String value, String valueType, String issuer, String originalIssuer, ClaimsIdentity subject, String propertyKey, String propertyValue) +14108789
   System.Security.Claims.Claim..ctor(String type, String value, String valueType) +62
   Microsoft.AspNet.Identity.<CreateAsync>d__0.MoveNext() +481
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +49
   Web.Controllers.<SignInAsync>d__42.MoveNext() in d:Google DriveDevelopmentGoalManagementWebControllersAccountController.cs:375
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
   Web.Controllers.<ExternalLoginConfirmation>d__35.MoveNext() in d:Google DriveDevelopmentGoalManagementWebControllersAccountController.cs:311
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84

public class IdentityUser : IUser
{
    public IdentityUser()
    {
        Logins = new List<IdentityUserLogin>();
    }

    public string Id { get; set; }
    public string UserName { get; set; }
    public string PasswordHash { get; set; }
    public string SecurityStamp { get; set; }
    public IList<IdentityUserLogin> Logins { get; set; }

}

public class IdentityUserLogin
{
    public string LoginProvider { get; set; }
    public string ProviderKey { get; set; }
}

如果需要,我可以包含我的用户存储代码:我没有将其放入,因为它是一个大文件,可能会影响问题.

I can include my userstore code if wanted: I didn't put it in as it's a large file and might detract from the issue.

我不确定为什么它甚至试图创建声明对象以及它为什么会爆炸.因为我只有 VS2012,所以主要是从网上的例子中修补.

I am not sure why it is even trying to create the claim object and why it is blowing up. As I only have VS2012 I have been patching it all together from examples online mainly.

按照@Shoe 的建议,我从 UserManager 继承:

As suggested by @Shoe I inherited from 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)
    {
        ClaimsIdentity identity = new ClaimsIdentity();
        return Task.FromResult(identity);
    }
}

它现在不再抛出错误,但实际上并没有验证我使用 Facebook 注册/登录的次数.

It now no longer throws an error but doesn't actually authenticate me how ever many times I use the Facebook register / login.

总结一下.使用@Shoe 的信息,我尝试使用以下方法覆盖 UserManager.CreateIdentityAsync:

To summarize. With @Shoe's info I tried both overriding UserManager.CreateIdentityAsync with:

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

并尝试使用返回的默认值(空列表)实现 IUserClaimStore.

and also trying to implement IUserClaimStore with the returning default (empty list).

第一个不会通过错误但最终不会通过身份验证.后者仍然会通过奇怪的声明System.Security.Claims.Claim..ctor"错误

The first will not through an error but does not end up authenticated. The later will still through the odd claim "System.Security.Claims.Claim..ctor" error

编辑

找出 ctor 错误发生的原因.用户对象在没有 ID 的情况下返回,因此默认的 UserManager 变得不安.修复了这个问题并使用了默认的 UserManager,它现在不再抛出错误,但仍然不会让用户登录.据我所知,它返回的身份对象看起来不错.

Found out why the ctor error was occurring. The user object was coming back without the ID so the default UserManager was getting upset. Fixed that and used the default UserManager which now no longer throws an error, but still doesn't log the user in. The identity object it returns looks good from what I can tell.

推荐答案

我过去也遇到过同样的错误,但仅在我使用 Entity Framework Migration Tool 创建用户时才出现.创建用户并在网站上签名时,我没有出错.

I had the same error in the past but only when I created user with Entity Framework Migration Tool. When creating a user and signing withing the website, I had not error.

我的错误是我没有为迁移提供SecurityStamp.

My error was that I was not providing a SecurityStamp with migration.

SecurityStamp = Guid.NewGuid().ToString()

这个属性集,一切正常.

This property set, everything worked.

这篇关于MVC5 (VS2012) 标识 CreateIdentityAsync - 值不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆