Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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# 理解MVC5用户索赔表_C#_Asp.net Mvc_Entity Framework_Asp.net Mvc 5_Asp.net Identity - Fatal编程技术网

C# 理解MVC5用户索赔表

C# 理解MVC5用户索赔表,c#,asp.net-mvc,entity-framework,asp.net-mvc-5,asp.net-identity,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc 5,Asp.net Identity,我已经做了很多研究,但没有一项结果能帮助我理解UserClaim表的意义 创建MVC5项目时,在注册数据库时会创建一些默认表。我理解所有这些的目的,但UserClaim除外 根据我的理解,用户声明基本上是关于用户的密钥对值。例如,如果我想要一个FavoriteBook字段,我可以将该字段添加到用户表中并访问它。事实上,我已经有了类似的内置功能。我的每个用户都有“自定义URL”,因此我用以下方式创建了一个声明: public class User : IdentityUser { publ

我已经做了很多研究,但没有一项结果能帮助我理解UserClaim表的意义

创建MVC5项目时,在注册数据库时会创建一些默认表。我理解所有这些的目的,但UserClaim除外

根据我的理解,用户声明基本上是关于用户的密钥对值。例如,如果我想要一个FavoriteBook字段,我可以将该字段添加到用户表中并访问它。事实上,我已经有了类似的内置功能。我的每个用户都有“自定义URL”,因此我用以下方式创建了一个声明:

public class User : IdentityUser
{
    public string CustomUrl { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        userIdentity.AddClaim(new Claim("CustomUrl", CustomUrl));
        return userIdentity;
    }
}

public static class UsersCustomUrl
{
    public static string GetCustomUrl(this IIdentity identity)
    {
        var claim = ((ClaimsIdentity)identity).FindFirst("CustomUrl");
        return (claim != null) ? claim.Value : string.Empty;
    }
}
公共类用户:IdentityUser
{
公共字符串CustomUrl{get;set;}
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
AddClaim(新声明(“CustomUrl”,CustomUrl));
返回用户身份;
}
}
公共静态类UsersCustomUrl
{
公共静态字符串GetCustomUrl(此IIIdentity标识)
{
var索赔=((索赔实体)标识).FindFirst(“自定义URL”);
return(claim!=null)?claim.Value:string.Empty;
}
}
基本上,我可以通过调用
User.Identity.GetCustomUrl()

上述代码不会写入UserClaims表,因为该值存在于Users表中。那么这张桌子的意义是什么呢


我在猜测,也许我应该将CustomUrl添加到UserClaims中,并以某种方式将其绑定到identity,这可能就是为什么?我很想知道答案

如果您提供多种方式让用户可以注册/登录您的网站,则声明非常有用。。。特别是,我指的是谷歌、Facebook和Twitter等组织的第三方认证

在用户通过其选择的第三方进行身份验证后,该第三方将向您披露一组声明,一组以您可以识别的方式描述用户的信息

索赔包含的信息因供应商而异。例如,谷歌将分享用户的电子邮件地址,他们的名字,姓氏,但将其与Twitter进行比较。。。Twitter不共享这些信息,您将收到他们的Twitter帐户的标识符以及他们的访问令牌


基于声明的身份验证提供了一种方便所有这些信息的简单方法,而另一种方法很可能意味着在数据库中为与您合作的每个提供商创建表。

这很有意义,谢谢您的澄清。