大佬教程收集整理的这篇文章主要介绍了asp.net-web-api – 如何自定义认证我自己的表在asp.net web api 2?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
public AccountController() : this(Startup.UseRMANagerFactory(),Startup.oAuthOptions.AccessTokenFormat) { }
在Startup.Auth.cs我看到
UseRMANagerFactory = () => new UseRMANager<IdentityUser>(new UserStore<IdentityUser>());
看起来像UserStore的实现来自Microsoft.AspNet.Identity.EntityFramework。
所以,要自定义认证,我必须实现我自己的版本的UserStore喜欢
class MYstuFFUserStore<IdentityUser> : UserStore<IdentityUser> { }
并覆盖方法,然后在Startup.Auth.cs中执行此操作
UseRMANagerFactory = () => new UseRMANager<IdentityUser>(new MYstuFFUserStore<IdentityUser>());
我正在寻找一种正确的方式来自定义身份验证。
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.@R_674_6048@s; 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中,将IdentityUser替换为AppUser
在AccountController.cs中,将IdentityUser替换为AppUser,并删除所有外部验证方法,如GetManageInfo和RegisterExternal等。
以上是大佬教程为你收集整理的asp.net-web-api – 如何自定义认证我自己的表在asp.net web api 2?全部内容,希望文章能够帮你解决asp.net-web-api – 如何自定义认证我自己的表在asp.net web api 2?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。