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. 使用Statement操作数据库

    i import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql ...

  2. H5 canvas填充文字自动换行

    canvas是H5中非常重要,非常常用,也是非常强大的一个新标签,美中不足的事,canvas中没没有自动换行的属性,我的第一反应是,字符串截取,然后计算每行的距离来实现自动换行.. 然后百度了一下,已 ...

  3. CAS 4.0.0RC 配置通过数据库认证用户登录

    配置通过数据库认证用户登录 打开webapp\WEB-INF目录下的deployerConfigContext.xml,替换 <bean id="primaryAuthenticati ...

  4. map() 函数

    map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例如,对于li ...

  5. poj 3067 - Japan(树状数组)

    先按第一个数从大到小排序,相等的情况下,第二个数按照从大到小排序..... 预处理后,照着树状数组写就行了... 注意:k的最大值应取1000*1000 代码如下: include <cstdi ...

  6. Spring 使用JSTL标签显示后台数据

    1. 先上项目结构图,其中config包下的代码文件参见前一篇博客   http://www.cnblogs.com/njust-ycc/p/6123505.html 引包: 2. 主要代码 (1)U ...

  7. C++混合编程之idlcpp教程Python篇(5)

    上一篇在这  C++混合编程之idlcpp教程Python篇(4) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程PythonTutorial3中,同样加入了三个文件:Py ...

  8. MediaWiki安装与配置(Ubuntu 10.4)

    实验室准备发布一个网站,本来是准备外包给别人做的,后来自己调研了一下,发现也没有想象的复杂和困难,于是最近一周自己吭哧吭哧地把网站搭好了. 之所以使用Mediawiki,一是考虑到是以实验室发布,不想 ...

  9. Ubuntu Server 15.04的安装

    U盘启动工具的制作就跟Windows系统以及Linux各版本的desktop版不同,用的工具也是我第一次见到的“Win32_Disk_Imager”(点击下载) 安装过程请参考:http://www. ...

  10. C#的函数柯里化

    前面说到了C#的泛型委托和闭包函数,在函数是程序设计里还有一个重要特征是柯里化... 柯里化就是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数且返回结 ...