在没有密码的情况下对Asp.net MVC 5中的USER进行身份验证 [英] Authenticate a USER in Asp.net MVC 5 without password
问题描述
我想仅使用用户名而不使用密码对用户进行身份验证.我的应用程序没有任何用户管理数据,我只想创建带有用户详细信息的身份,以便可以在应用程序中使用它.
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.
我试图复制SingInAsync方法来解决这个问题
I tried to copy the SingInAsync method to put this up
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);
}
但是它告诉我并且出现错误-找不到用户ID .有什么办法我可以通过用户名对用户进行身份验证并为身份分配一些详细信息吗?
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??
推荐答案
没有用户,只需忘记UserManager并自己创建ClaimsIdentity
.
With no users just forget about the UserManager and create the ClaimsIdentity
yourself.
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.
这篇关于在没有密码的情况下对Asp.net MVC 5中的USER进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!