gpt4 book ai didi

asp.net-web-api - 如何在asp.net web api 2中自定义我自己的一组表的身份验证?

转载 作者:行者123 更新时间:2023-12-03 10:24:07 26 4
gpt4 key购买 nike

在创建的默认 AccountController 中,我看到

    public AccountController()
: this(Startup.UserManagerFactory(), Startup.OAuthOptions.AccessTokenFormat)
{
}

在 Startup.Auth.cs 我看到
    UserManagerFactory = () => 
new UserManager<IdentityUser>(new UserStore<IdentityUser>());

似乎是 的实现用户商店 来自 Microsoft.AspNet.Identity.EntityFramework .

因此,要自定义身份验证,我是否必须实现我自己的 UserStore 版本,例如
 class MYSTUFFUserStore<IdentityUser> : UserStore<IdentityUser>
{
}

并覆盖方法,然后在 Startup.Auth.cs 中执行此操作
UserManagerFactory = () => 
new UserManager<IdentityUser>(new MYSTUFFUserStore<IdentityUser>());

我正在寻找一种正确的方法来自定义身份验证。

最佳答案

假设您的表名为 AppUser ,转换您自己的AppUser域对象到 IUser(using Microsoft.AspNet.Identity)像这样

using Microsoft.AspNet.Identity;
public class AppUser : IUser
{
//Existing database fields
public long AppUserId { get; set; }
public string AppUserName { get; set; }
public string AppPassword { get; set; }

public AppUser()
{
this.Id = Guid.NewGuid().ToString();
}

[Ignore]
public virtual string Id { get; set; }
[Ignore]
public string UserName
{
get
{
return AppUserName;
}
set
{
AppUserName = value;
}
}
}

实现 UserStore像这样的对象
using Microsoft.AspNet.Identity;
public class UserStoreService
: IUserStore<AppUser>, IUserPasswordStore<AppUser>
{
CompanyDbContext context = new CompanyDbContext();

public Task CreateAsync(AppUser user)
{
throw new NotImplementedException();
}

public Task DeleteAsync(AppUser user)
{
throw new NotImplementedException();
}

public Task<AppUser> FindByIdAsync(string userId)
{
throw new NotImplementedException();
}

public Task<AppUser> FindByNameAsync(string userName)
{
Task<AppUser> task = context.AppUsers.Where(
apu => apu.AppUserName == userName)
.FirstOrDefaultAsync();

return task;
}

public Task UpdateAsync(AppUser user)
{
throw new NotImplementedException();
}

public void Dispose()
{
context.Dispose();
}

public Task<string> GetPasswordHashAsync(AppUser user)
{
if (user == null)
{
throw new ArgumentNullException("user");
}

return Task.FromResult(user.AppPassword);
}

public Task<bool> HasPasswordAsync(AppUser user)
{
return Task.FromResult(user.AppPassword != null);
}

public Task SetPasswordHashAsync(AppUser user, string passwordHash)
{
throw new NotImplementedException();
}

}

如果您有自己的自定义密码散列,您还需要实现 IPasswordHasher .下面是一个没有密码散列的示例(哦,不!)
using Microsoft.AspNet.Identity;
public class MyPasswordHasher : IPasswordHasher
{
public string HashPassword(string password)
{
return password;
}

public PasswordVerificationResult VerifyHashedPassword
(string hashedPassword, string providedPassword)
{
if (hashedPassword == HashPassword(providedPassword))
return PasswordVerificationResult.Success;
else
return PasswordVerificationResult.Failed;
}
}

在 Startup.Auth.cs 中替换
UserManagerFactory = () => 
new UserManager<IdentityUser>(new UserStore<IdentityUser>());


    UserManagerFactory = () => 
new UserManager<AppUser>(new UserStoreService()) { PasswordHasher = new MyPasswordHasher() };

ApplicationOAuthProvider.cs , 替换 IdentityUserAppUser
AccountController.cs , 替换 IdentityUserAppUser并删除所有外部身份验证方法,如 GetManageInfoRegisterExternal等等。

关于asp.net-web-api - 如何在asp.net web api 2中自定义我自己的一组表的身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20529401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com