首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用索赔标识将用户身份验证到系统中

使用索赔标识将用户身份验证到系统中
EN

Stack Overflow用户
提问于 2016-01-04 10:06:54
回答 1查看 7.6K关注 0票数 4

我需要登录一个用户(在用户名的帮助下)通过索赔身份和以下是什么是我试图实现的系统。

  1. 在用户名的帮助下,从数据库中获取用户详细信息并创建用户对象。
  2. 并将对象传递给CreateIdentityAsync

DefaultAuthenticationTypes.ApplicationCookie);UserManager.CreateIdentityAsync(用户)

目前,应用程序已启用多租户。上面的方法只适用于租户Id为null的记录。但对于其他具有tenent id null的有效记录,则是抛出错误

userId未找到

来自userManager.CreateIdentityAsync

因此,我尝试创建一个自定义的声明标识并登录到系统中,如下所示

代码语言:javascript
复制
        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);

        List<Claim> claims = new List<Claim>{
    new Claim(ClaimTypes.GivenName, newUser.Name), //user.Name from my database
    new Claim(ClaimTypes.NameIdentifier, newUser.Id.ToString()), //user.Id from my database
    new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "MyApplicationName"),      
    new Claim(ClaimTypes.Email, newUser.EmailAddress),
    new Claim(ClaimTypes.Surname, newUser.Surname)
};
        ClaimsIdentity identity = new System.Security.Claims.ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie, ClaimTypes.Name, ClaimTypes.Role);

        AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity);

也因为某种原因而失败了。

有人能帮我解决这个问题吗。如何通过索赔身份将用户登录到系统

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-05 09:10:49

通过查看Asp.net身份框架是如何实现索赔标识的。我成功地创建了一个自定义声明标识,如下所示。

代码语言:javascript
复制
    string IdentityProviderClaimType =
    "http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider";
    string DefaultIdentityProviderClaimValue = "ASP.NET Identity";

    var id = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
    id.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString(), ClaimValueTypes.String));
    id.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, user.UserName, ClaimValueTypes.String));
    id.AddClaim(new Claim(IdentityProviderClaimType, DefaultIdentityProviderClaimValue, ClaimValueTypes.String));
   //Provide the below if you have any role name 
        id.AddClaim(new Claim(ClaimsIdentity.DefaultRoleClaimType,role.Name , ClaimValueTypes.String));

希望这能帮上忙。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34588666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档