PasswordHasher
namespace Microsoft.AspNet.Identity
{
public class PasswordHasher : IPasswordHasher
{
public virtual string HashPassword(string password)
{
return Crypto.HashPassword(password);
} public virtual PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword)
{
if (Crypto.VerifyHashedPassword(hashedPassword, providedPassword))
{
return PasswordVerificationResult.Success;
}
return PasswordVerificationResult.Failed;
}
}
}
using System;
using System.Runtime.CompilerServices;
using System.Security.Cryptography; namespace Microsoft.AspNet.Identity
{
internal static class Crypto
{
private const int PBKDF2IterCount = 1000; private const int PBKDF2SubkeyLength = 32; private const int SaltSize = 16; public static string HashPassword(string password)
{
if (password == null)
{
throw new ArgumentNullException("password");
}
byte[] salt;
byte[] bytes;
using (Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, 16, 1000))
{
salt = rfc2898DeriveBytes.Salt;
bytes = rfc2898DeriveBytes.GetBytes(32);
}
byte[] array = new byte[49];
Buffer.BlockCopy(salt, 0, array, 1, 16);
Buffer.BlockCopy(bytes, 0, array, 17, 32);
return Convert.ToBase64String(array);
} public static bool VerifyHashedPassword(string hashedPassword, string password)
{
if (hashedPassword == null)
{
return false;
}
if (password == null)
{
throw new ArgumentNullException("password");
}
byte[] array = Convert.FromBase64String(hashedPassword);
if (array.Length != 49 || array[0] != 0)
{
return false;
}
byte[] array2 = new byte[16];
Buffer.BlockCopy(array, 1, array2, 0, 16);
byte[] array3 = new byte[32];
Buffer.BlockCopy(array, 17, array3, 0, 32);
byte[] bytes;
using (Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, array2, 1000))
{
bytes = rfc2898DeriveBytes.GetBytes(32);
}
return Crypto.ByteArraysEqual(array3, bytes);
} [MethodImpl(MethodImplOptions.NoOptimization)]
private static bool ByteArraysEqual(byte[] a, byte[] b)
{
if (object.ReferenceEquals(a, b))
{
return true;
}
if (a == null || b == null || a.Length != b.Length)
{
return false;
}
bool flag = true;
for (int i = 0; i < a.Length; i++)
{
flag &= (a[i] == b[i]);
}
return flag;
}
}
}
PasswordHasher的更多相关文章
- PasswordHasher 算法
public override PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string provid ...
- ASP.NET Core 之 Identity 入门(三)
前言 在上一篇文章中,我们学习了 CookieAuthentication 中间件,本篇的话主要看一下 Identity 本身. 最早2005年 ASP.NET 2.0 的时候开始, Web 应用程序 ...
- [转]ASP.NET Core 之 Identity 入门(三)
本文转自:http://www.cnblogs.com/savorboard/p/aspnetcore-identity3.html 前言 在上一篇文章中,我们学习了 CookieAuthentica ...
- Microsoft.AspNet.Identity 自定义使用现有的表—登录实现
Microsoft.AspNet.Identity是微软新引入的一种membership框架,也是微软Owin标准的一个实现.Microsoft.AspNet.Identity.EntityFrame ...
- Migrating an Existing Website from SQL Membership to ASP.NET Identity
Migrating an Existing Website from SQL Membership to ASP.NET Identity public class User : IdentityUs ...
- ASP.NET MVC 随想录——开始使用ASP.NET Identity,初级篇
在之前的文章中,我为大家介绍了OWIN和Katana,有了对它们的基本了解后,才能更好的去学习ASP.NET Identity,因为它已经对OWIN 有了良好的集成. 在这篇文章中,我主要关注ASP. ...
- 【ASP.NET Identity系列教程(一)】ASP.NET Identity入门
注:本文是[ASP.NET Identity系列教程]的第一篇.本系列教程详细.完整.深入地介绍了微软的ASP.NET Identity技术,描述了如何运用ASP.NET Identity实现应用程序 ...
- 自定义表并实现Identity登录(一)
注意,Microsoft.AspNet.Identity.Core.1.0.0和Microsoft.AspNet.Identity.Core.2.2.1差别太大,需考虑实际项目中用的是哪种,本文是基于 ...
- ASP.NET MVC 随想录——开始使用ASP.NET Identity,初级篇(转)
ASP.NET MVC 随想录——开始使用ASP.NET Identity,初级篇 阅读目录 ASP.NET Identity 前世今生 建立 ASP.NET Identity 使用ASP.NET ...
随机推荐
- IOS APP开发中View的几种实现方式
xib文件有以下几个重要的属性: xib文件名 File’s Owner xib文件中的视图的Class xib文件中的视图的Outlet指向 File’s Owner 可以关联到某类,然后通过IBO ...
- C#-WinForm-公共控件的基本属性及练习
视图→工具箱 基本操作:控件的取值.赋值.改值.事件 1.Button --按钮 AutoSize-指示该控件是否自动调整自身的大小以适应其内容的大小. 默认False,此时文字内容超过其宽度时自动 ...
- UIView的autoresizingMask和autoresizesSubviews属性的剖析
UIVIew的autoresizingMask和autoresizesSubviews属性的剖析 autoresizingMask是为了iPad开发中横竖屏适配而降生的,他只能约束父子控件之间的关系. ...
- python协程和yeild
python多线程其实在操作系统级别是进程,因为在执行时,默认加了一个全局解释器锁(GIL),python的多线程,本质还是串行的,无法利用多核的优势:在java和C# 中,多线程是并发的,可以充分利 ...
- cmd 利用IE打开网页
"C:\Program Files\Internet Explorer\iexplore.exe" "http://dmsite.chinacloudsites.cn/r ...
- CLR/.NET/C#/Visual Studio/ASP.NET各版本之间的关系(转)
由于这篇文章记录的是2015年7月,那时.net core还是叫做.net core 5 名词定义 下列这些名词,写.NET 的人一定都不陌生,但你是否有真正理解呢?如果看了我的摘要文字说明还无法理解 ...
- Jenkins参数化构建插件,实现构建前输入自定义参数
插件: [Build with Parameters]:https://wiki.jenkins-ci.org/display/JENKINS/Build+With+Parameters+Plugin ...
- 利用TabHost制作QQ客户端标签栏效果(低版本QQ)
学习一定要从基础学起,只有有一个好的基础,我们才会变得更加的perfect 下面小编将利用TabHost制作QQ客户端标签栏效果(这个版本的QQ是在前几年发布的)…. 首先我们看一下效果: 看到这个界 ...
- MongoDB: CURD操作
>> 创建:·db.foo.insert({"bar":"baz"}) //如果文档中没有"_id"键会自动增加一个·db.fo ...
- 做自己的类库dll文件
除了在项目中把类放在不同的文件夹之外,还可以把他们放在完全不同的项目中,添加引用dll 实现对类的调用, 如果一个项目只包含类 但没有入口点 ,那么这个项目叫做类库. 类库项目编译为.dll程序集.( ...