I want to authenticate a user with only user name and no password. My application does not have any user management data and I just want to create Identity with user details so that I can use it in the application.
我想验证只有用户名和密码的用户。我的应用程序没有任何用户管理数据,我只想创建具有用户详细信息的Identity,以便我可以在应用程序中使用它。
I tried to copy the SingInAsync method to put this up
我试图复制SingInAsync方法来解决这个问题
private async Task InitializeUser()
{
var user = new ApplicationUser();
user.Id = "abcd";
user.UserName = "abcd";
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity);
}
But it tells me and error - that user ID cannot be found. Is there someway I can just authenticate the user by username and assign the Identity with some details??
但它告诉我和错误 - 无法找到用户ID。有没有我可以通过用户名验证用户并为身份分配一些细节?
1 个解决方案
#1
With no users just forget about the UserManager and create the ClaimsIdentity
yourself.
没有用户只是忘记了UserManager并自己创建了ClaimsIdentity。
private async Task InitializeUser()
{
var user = new ApplicationUser();
user.Id = "abcd";
user.UserName = "abcd";
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
var id = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
id.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", ClaimValueTypes.String));
id.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, user.UserName, ClaimValueTypes.String));
id.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id, ClaimValueTypes.String));
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, id);
}
You probably want to tweak this a bit to make it cleaner.
您可能想稍微调整一下以使其更清洁。
#1
With no users just forget about the UserManager and create the ClaimsIdentity
yourself.
没有用户只是忘记了UserManager并自己创建了ClaimsIdentity。
private async Task InitializeUser()
{
var user = new ApplicationUser();
user.Id = "abcd";
user.UserName = "abcd";
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
var id = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
id.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", ClaimValueTypes.String));
id.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, user.UserName, ClaimValueTypes.String));
id.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id, ClaimValueTypes.String));
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, id);
}
You probably want to tweak this a bit to make it cleaner.
您可能想稍微调整一下以使其更清洁。