WebAPI 2.x中如何扩展Identity Store
ASP.NET WebAPI 中引入了新的一套身份验证和授权的机制,官方的叫法是ASP.NET Identity,有关这个概念的细节,感兴趣的同学可以参考 http://www.asp.net/identity
这套新的机制,默认还是使用SQL Server来做身份保存的,但更多的是提供了灵活性,包括与外部验证系统(OAuth)的整合。但在一些较为简单的场合下,我们可能希望简化这个部分,例如我们不需要外部整合,而且我们的用户数也相对有限,不希望用数据库来实现。
本文提供了一个实例,我是使用XML文件的方式来保存用户信息的,该文件的格式大致如下
然后,我编写了一个自定义的类型,实现了一些主要的方法
public class XmlUserStore : IUserStore<ApplicationUser>,IUserPasswordStore<ApplicationUser>,IRoleStore<IdentityRole> { private string filePath = HttpContext.Current.Server.MapPath("~/app_data/users.xml"); public Task CreateAsync(ApplicationUser user) { throw new System.NotImplementedException(); } public Task DeleteAsync(ApplicationUser user) { throw new System.NotImplementedException(); } public Task<ApplicationUser> FindByIdAsync(string userId) { return FindByNameAsync(userId); } public Task<ApplicationUser> FindByNameAsync(string userName) { var doc = XDocument.Load(filePath); var found = doc.Root.Elements("user").FirstOrDefault(x => x.Attribute("name").Value == userName); ApplicationUser user = null; if (found != null) { user = new ApplicationUser() { UserName = userName, Id = userName, PasswordHash = found.Attribute("password").Value }; } return Task<ApplicationUser>.FromResult(user); } public Task UpdateAsync(ApplicationUser user) { return Task.FromResult(0); } public void Dispose() { } public Task<string> GetPasswordHashAsync(ApplicationUser user) { var result = string.Empty; if (user != null) result = user.PasswordHash; return Task<string>.FromResult(result); } public Task<bool> HasPasswordAsync(ApplicationUser user) { throw new System.NotImplementedException(); } public Task SetPasswordHashAsync(ApplicationUser user, string passwordHash) { var doc = XDocument.Load(filePath); var found = doc.Root.Elements("user").FirstOrDefault(x => x.Attribute("name").Value ==user.UserName); if(found!=null) { found.Attribute("password").Value = passwordHash; doc.Save(filePath); return Task.FromResult(1); } return Task.FromResult(0); } public Task CreateAsync(IdentityRole role) { throw new System.NotImplementedException(); } public Task DeleteAsync(IdentityRole role) { throw new System.NotImplementedException(); } Task<IdentityRole> IRoleStore<IdentityRole>.FindByIdAsync(string roleId) { throw new System.NotImplementedException(); } Task<IdentityRole> IRoleStore<IdentityRole>.FindByNameAsync(string roleName) { throw new System.NotImplementedException(); } public Task UpdateAsync(IdentityRole role) { throw new System.NotImplementedException(); } }
接下来,我们要在AccountController中使用这个新的UserStore的类型。
分类:
ASP.NET MVC
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)
2011-03-16 SQL Server 2008性能数据收集(Data Collector)的一些扩展话题
2009-03-16 IE 6和IE 7对于按钮背景图案的处理 【转载】
2009-03-16 IE 6和IE 7对于按钮背景图案的处理 【转载】