asp.net-web-api – 如何自定义认证我自己的表在asp.net web api 2?
发布时间:2020-05-23 11:01:32 所属栏目:asp.Net 来源:互联网
导读:在默认AccountController创建我看到 public AccountController() : this(Startup.UserManagerFactory(), Startup.OAuthOptions.AccessTokenFormat) { } 在Startup.Auth.cs我看到 UserManagerFactory = () =
|
在默认AccountController创建我看到 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>());
我正在寻找一种正确的方式来自定义身份验证。 解决方法假设您的表称为AppUser,将您自己的AppUser域对象转换为IUser(使用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中,将IdentityUser替换为AppUser 在AccountController.cs中,将IdentityUser替换为AppUser,并删除所有外部验证方法,如GetManageInfo和RegisterExternal等。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 从传递给局部视图的嵌套复杂对象获取值
- asp.net-mvc – MVC5注销链接从不同区域失败
- asp.net – 检查Active Directory密码是否与cookie不同
- asp.net – 将网站置于维护模式?
- asp.net-core – 如何使用带有IdentityServer4的ASP.Net标识
- 从ASP.NET 3.5应用程序在IFRAME中运行GWT应用程序(包括Appl
- 在ASP.NET 5中获取AuthenticationProperties
- asp.net – ‘Owin.IAppBuilder’不包含’MapSignalR’的定
- asp.net – 在web.config文件中设置重定向
- asp.net-mvc – ASP.NET MVC WebSite中的ERR_EMPTY_RESPONS
推荐文章
站长推荐
- asp.net – 可以在机器之间复制Cookie以假冒用户
- 在加载asp.net页面时显示gif
- asp.net – 为什么要模拟HttpContext,如果它可以
- asp.net – 什么Jenkins插件可以用于.NET网站部署
- asp.net-mvc-5 – aspnet身份避免同时登录同一帐
- asp.net-web-api – 我可以在WebAPI messageHand
- asp.net – 从静态类访问Page的当前实例
- asp.net – LinkButton CommandName和CommandArg
- 为什么ASP.NET MVC中的移动视图在不同的服务器上
- asp.net-mvc-3 – 如何在razor视图中设置@ model
热点阅读
