Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义应用程序用户asp.net-identity_C#_Asp.net Identity - Fatal编程技术网

C# 自定义应用程序用户asp.net-identity

C# 自定义应用程序用户asp.net-identity,c#,asp.net-identity,C#,Asp.net Identity,我正在尝试将对象列表添加到MVC中的标准ApplicationUser对象中 我已经看了几十个关于这方面的问题,但它们似乎都在添加一个对象,而不是一个列表 我试图做的是记录用户使用过的所有历史密码,因此我添加了一个名为AspNetUserPreviousPassword的表 在我的代码中,我已将这一行添加到ApplicationUser类中: public virtual DbSet<AspNetUserPreviousPassword> PreviousUserPasswords

我正在尝试将对象列表添加到MVC中的标准ApplicationUser对象中

我已经看了几十个关于这方面的问题,但它们似乎都在添加一个对象,而不是一个列表

我试图做的是记录用户使用过的所有历史密码,因此我添加了一个名为
AspNetUserPreviousPassword
的表

在我的代码中,我已将这一行添加到
ApplicationUser
类中:

public virtual DbSet<AspNetUserPreviousPassword> PreviousUserPasswords { get; set; }
我创建了以下扩展类:

出现在
User.Identity
intellisense中的公共静态类IdentityExtensions-到目前为止还不错

{
    public static string GetPreviousUserPasswords(this IIdentity identity)
    {
        var claim = ((ClaimsIdentity)identity).FindFirst("PreviousUserPasswords");
        // Test for null to avoid issues during local testing
        return (claim != null) ? claim.Value : string.Empty;
    }
}
当我去编辑
GenerateUserIdentityAsync
函数以插入自定义声明时,我开始认为我的方法不正确,因为只能添加字符串,例如

userIdentity.AddClaim(new Claim("PreviousUserPasswords", "This must be a string"));
尽管只能在此处添加字符串,但我想测试以前的密码是否从数据库中读取,因此我添加了以下代码以测试:

string previousPasswords = "";
await this.PreviousUserPasswords.ForEachAsync(p => previousPasswords += p.PasswordHash);
此.PreviousUserPasswords
始终为
NULL

我的问题是:

1) 为什么
this.PreviousUserPasswords
总是
NULL


2) 我的方法是否正确?我是否可以将对象列表添加到
ApplicationUser
中,还是应该以另一种方式执行此操作?

以下是我用来防止在Asp.Net Identity实现中使用以前的“X”密码的文章。我对它进行了调整以满足我的需要,但这让我得到了一切,我需要得到正确的轨道。读一读


太好了,我来看看。我几乎实现了我自己的方法,但它不像让用户包含以前的密码列表那样干净,但它似乎做到了这一点,无论如何,到目前为止……对我来说效果非常好。我可以为每个客户端设置自己的密码策略要求,包括定义用户不能使用最后的“X”密码。如果这有帮助,请标记为已接受答案;)
string previousPasswords = "";
await this.PreviousUserPasswords.ForEachAsync(p => previousPasswords += p.PasswordHash);