[英]Authenticate User into the System using Claims Identity
I Need to login a user (with help of user name) to the system through claims identity and below is what am trying to achieve. 我需要通过声明身份将用户(在用户名的帮助下)登录到系统,以下是要实现的目标。
With the help of a User Name fetch the user details from Database and create a user object. 在用户名的帮助下,从数据库中获取用户详细信息并创建用户对象。
And passing the object to the CreateIdentityAsync 并将对象传递给CreateIdentityAsync
UserManager.CreateIdentityAsync(user,DefaultAuthenticationTypes.ApplicationCookie);
UserManager.CreateIdentityAsync(用户,DefaultAuthenticationTypes.ApplicationCookie);
Currently the application is multi-tenant enabled. 当前,该应用程序已启用多租户。 The above method works only for the records where tenant Id is null.
上面的方法仅适用于租户ID为空的记录。 But for other valid records with tenent id not null , it's throwing error
但是,对于其他具有租户ID不为null的有效记录,则会引发错误
userId not found
找不到userId
from userManager.CreateIdentityAsync 来自userManager.CreateIdentityAsync
So I tried creating a custom claim identity and login into the system as below 因此,我尝试创建一个自定义声明标识并按如下所示登录系统
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);
Which is also failing due to some reason. 由于某些原因,它也失败了。
Can anybody help me solve this issue. 谁能帮我解决这个问题。 How can I login a user to the system through claims identity
如何通过声明身份将用户登录到系统
By Looking at how Asp.net Identity Framework implemented the Claims Identity . 通过查看Asp.net Identity Framework如何实现Claims Identity。 I was able to successfully create an custom Claims Identity as below.
我能够成功创建一个自定义的Claims Identity,如下所示。
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));
Hope this helps someone. 希望这对某人有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.