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();
}
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

接下来,我们要在AccountController中使用这个新的UserStore的类型。

WebAPI 2.x中如何扩展Identity Store的更多相关文章

  1. 解决ora-01652无法通过128(在temp表空间中)扩展temp段的过程

    解决ora-01652无法通过128(在temp表空间中)扩展temp段的过程 昨天开发人员跟我说,执行一个sql语句后,大约花了10分钟,好不容易有一个结果,但是报了一个ora-01652错误,查阅 ...

  2. 如果正确读取SQL Server中的扩展事件?

        SQL Server中使用扩展事件捕捉所需的信息后,可以选择存放的位置.比如说内存或文件中,但无论存在哪里,其本质都是一个大XML.因此在SQL Server中读取该XML就是解析扩展事件结果 ...

  3. oracle暂时表空间 ORA-01652:无法通过16(在表空间XXX中)扩展 temp 字段

    今天在查数据的时候报错  ORA-01652:无法通过16(在表空间temp1中)扩展 temp 字段 查看表空间使用明细 SELECT b.tablespace,        b.segfile# ...

  4. Access中的SELECT @@IDENTITY

    在Access数据库中存在select @@identity吗?答案是肯定的.但是Access一次只能执行一条SQL,多条SQL需要多次执行,这是限制.在SQL Server中,可以一次执行多条SQL ...

  5. ORA-01653:表无法通过64(在表空间USERS中)扩展

    问题描述:oracle插入数据时显示ORA-01653 表无法通过64(在表空间USERS中)扩展 原因:  oracle  表空间满了,需要扩展 截图: 解决方法: 1.首先查下表空间 select ...

  6. C#3.0中的扩展方法

    在实际应用中,开发者完成代码的编译后,除非重新编译更改后的代码,否则开发者很难在原有代码中添加新的功能. 在C#3.0中,提供了一个扩展方法的新特性,可以使得开发者在编译后的程序集里边添加相关的方法, ...

  7. AutoCAD中的扩展字典及扩展记录(C#)

    在学习CAD扩展记录的过程中,遇到了一些问题,也积累了一些经验,现在给大家分享一些我的学习心得.在学习扩展字典之前需要读者了解cad的组码,也就是DxfCode.感兴趣的也可以了解一下扩展数据的相关内 ...

  8. oracle临时表空间 ORA-01652:无法通过16(在表空间XXX中)扩展 temp 字段

    今天在查数据的时候报错  ORA-01652:无法通过16(在表空间temp1中)扩展 temp 字段 查看表空间使用明细 SELECT b.tablespace,        b.segfile# ...

  9. spring-boot的spring-cache中的扩展redis缓存的ttl和key名

    原文地址:spring-boot的spring-cache中的扩展redis缓存的ttl和key名 前提 spring-cache大家都用过,其中使用redis-cache大家也用过,至于如何使用怎么 ...

随机推荐

  1. Pip Permittion Issue on MacOS

    Question: OSError: [Errno 1] Operation not permitted: '/tmp/pip-W13DsU-uninstall/System/Library/Fram ...

  2. list-style

    list-style: inside url(" "); 默认li的点在父级div框的外面,在li上加样式list-style-position: inside;可以使点在框内:点 ...

  3. [ASE][Daily Scrum]12.05

    占坑 最近大家都很忙所以工作准备放到周末来做……所以这两天进度会比较慢.

  4. 如何安全地关闭MySQL实例

    如何安全地关闭MySQL实例 转载自:http://imysql.com/2014/08/13/mysql-faq-howto-shutdown-mysqld-fulgraceful.shtml 本文 ...

  5. 【TextBox】重写右键菜单

    参考资料 http://bbs.csdn.net/topics/390324356 http://www.cnblogs.com/ycxy/archive/2012/10/09/2716852.htm ...

  6. ASP.NET 5 入门 (2) – 自定义配置

    ASP.NET 5 入门 (2) – 自定义配置 ASP.NET 5 理解和入门 建立和开发ASP.NET 5 项目 初步理解ASP.NET5的配置 正如我的第一篇文章ASP.NET 5 (vNext ...

  7. SVN分支管理策略个人见解

    本篇目录 前言 SVN分支管理策略 VisualSVN Server TortoiseSVN客户端 Repository的创建 Check out trunk创建新项目MyProject trunk更 ...

  8. java中基本类型和包装类型实践经验

    至今,小菜用java快两年了,有些事,也该有个总结. 基本类型和包装类型的概念在本文不作赘述. 如果这两种类型直接使用,倒没什么值得讨论的,无非就是自动装箱拆箱,java可以让你感觉不到他们的存在,但 ...

  9. JavaScript字符转Unicode,顺便说句:GitHub的Oh no页面很亮

    遇到个输不出来的字符怎么办,因为输不出来的字符一般又是不常见大多数时候连名字也喊不出来的,所以想问百度谷歌大大也不大可能.如果是小白用户肯定会去把输入法软盘打开切换到其他键盘一个一个找.即使有搜狗输入 ...

  10. Winform文件下载之WinINet

    在C#中,除了webclient我们还可以使用一组WindowsAPI来完成下载任务.这就是Windows Internet,简称 WinINet.本文通过一个demo来介绍WinINet的基本用法和 ...