asp.net-web-api – 如何自定义认证我自己的表在asp.net web api
发布时间:2020-12-15 18:51:18 所属栏目:asp.Net 来源:网络整理
导读:在默认AccountController创建我看到 public AccountController() : this(Startup.UserManagerFactory(),Startup.OAuthOptions.AccessTokenFormat) { } 在Startup.Auth.cs我看到 UserManagerFactory = () = new UserManagerIdentityUser(new UserStoreIdentit
|
在默认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通过ajax调用后台方法
- asp.net – Web发布的密码不同于我的Azure管理员密码?
- asp.net-mvc – 以视图的形式显示视图中多个表的数据 – AS
- entity-framework-core – 实体框架Core 2.0许多表中公共属
- asp.net-mvc – 在DropDownList ASP.NET MVC中获取所选项目
- asp.net-mvc – 这叫什么类型的架构?
- asp.net-mvc – ASP.Net MVC中的多项目领域3
- 如何创建ASP.NET RecaptchaControl自定义模板
- asp.net – IIS分段和生产交换
- asp.net-core – vNext MVC模板 – wwwroot
推荐文章
站长推荐
- asp.net – 加密ASP .NET 2.0和SQL Server 2005中
- asp.net-mvc – .NET MVC / Entity Framework应用
- 在ASP.NET核心中间件中设置响应状态
- asp.net-mvc – FluentValidation可以做复选框吗
- 部署ASP.Net MVC应用程序的最便宜的方法是什么?
- asp.net-mvc – 无法安装Asp.net MVC 3
- asp.net – 在更改密码时从所有浏览器注销用户
- ASP.Net MVC 引用动态 js 脚本
- 如何在asp.net中使用ccavenue支付网关
- ASP.NET – 如何使用CSS在数据条目中对齐ASP.NET
热点阅读
