sun*_*nil 26 asp.net-web-api asp.net-identity
在我看到的默认AccountController中创建
public AccountController()
: this(Startup.UserManagerFactory(), Startup.OAuthOptions.AccessTokenFormat)
{
}
Run Code Online (Sandbox Code Playgroud)
在Startup.Auth.cs中我看到了
UserManagerFactory = () =>
new UserManager<IdentityUser>(new UserStore<IdentityUser>());
Run Code Online (Sandbox Code Playgroud)
似乎UserStore的实现来自Microsoft.AspNet.Identity.EntityFramework.
因此,要自定义身份验证,我必须实现自己的UserStore版本
class MYSTUFFUserStore<IdentityUser> : UserStore<IdentityUser>
{
}
Run Code Online (Sandbox Code Playgroud)
并覆盖方法,然后在Startup.Auth.cs中执行此操作
UserManagerFactory = () =>
new UserManager<IdentityUser>(new MYSTUFFUserStore<IdentityUser>());
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种自定义身份验证的正确方法.
sun*_*nil 44
假设您的表被调用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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
如果您有自己的自定义密码哈希,则还需要实现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;
}
}
Run Code Online (Sandbox Code Playgroud)
在Startup.Auth.cs中替换
UserManagerFactory = () =>
new UserManager<IdentityUser>(new UserStore<IdentityUser>());
Run Code Online (Sandbox Code Playgroud)
同
UserManagerFactory = () =>
new UserManager<AppUser>(new UserStoreService()) { PasswordHasher = new MyPasswordHasher() };
Run Code Online (Sandbox Code Playgroud)
在ApplicationOAuthProvider.cs,替换IdentityUser为AppUser
在AccountController.cs,替换IdentityUser用AppUser,并删除所有外部身份验证方法,如GetManageInfo和RegisterExternal等.
| 归档时间: |
|
| 查看次数: |
26463 次 |
| 最近记录: |