我需要登录一个用户(在用户名的帮助下)通过索赔身份和以下是什么是我试图实现的系统。
DefaultAuthenticationTypes.ApplicationCookie);UserManager.CreateIdentityAsync(用户)
目前,应用程序已启用多租户。上面的方法只适用于租户Id为null的记录。但对于其他具有tenent id null的有效记录,则是抛出错误
userId未找到
来自userManager.CreateIdentityAsync
因此,我尝试创建一个自定义的声明标识并登录到系统中,如下所示
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);也因为某种原因而失败了。
有人能帮我解决这个问题吗。如何通过索赔身份将用户登录到系统
发布于 2016-01-05 09:10:49
通过查看Asp.net身份框架是如何实现索赔标识的。我成功地创建了一个自定义声明标识,如下所示。
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));希望这能帮上忙。
https://stackoverflow.com/questions/34588666
复制相似问题