CQRS学习——集成ASP.NET Identity[其五]
【其实和Cqrs没啥关系】
缘由
其实没啥原因,只是觉得以前写了不知多少遍的用户登录复用性太差,实现的功能也不多。
依赖的Nuget包
简单登陆
就简单登陆而言,只需要实现如下接口/抽象类:
Store相关:
IUserLockoutStore<DpfbUser,Guid> , IUserPasswordStore<DpfbUser,Guid>, IUserTwoFactorStore<DpfbUser,Guid>, IUserEmailStore<DpfbUser,Guid>
Manager相关:
UserManager<DpfbUser, Guid>, SignInManager<DpfbUser, Guid>
打包的代码:
public class AppSignInManager : SignInManager<DpfbUser, Guid>
{
public AppSignInManager()
: base(WebContextHelper.CurrentOwinContext.Get<AppUserManager>(),
WebContextHelper.CurrentOwinContext.Authentication)
{ } public override async Task<ClaimsIdentity> CreateUserIdentityAsync(DpfbUser user)
{
var userIdentity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
} public class AppUserManager : UserManager<DpfbUser, Guid>
{
public AppUserManager(DpfbUserStore store)
: base(store)
{ } public AppUserManager()
: this(WebContextHelper.CurrentOwinContext.Get<DpfbUserStore>())
{ }
} public class DpfbUserStore :
//IUserStore<DpfbUser, Guid>,
IUserLockoutStore<DpfbUser, Guid>,
IUserPasswordStore<DpfbUser,Guid>,
IUserTwoFactorStore<DpfbUser,Guid>,
IUserEmailStore<DpfbUser,Guid>
{
[Dependency]
internal IDpfbUserQueryEntry UserQueryEntry
{
get { return CqrsConfigurationResolver.Config.Construct<IDpfbUserQueryEntry>(); }
} internal ICommandBus CommandBus
{
get { return CqrsConfigurationResolver.CommandBus; }
} public Task CreateAsync(DpfbUser user)
{
throw new NotImplementedException();
} public Task DeleteAsync(DpfbUser user)
{
throw new NotImplementedException();
} public Task<DpfbUser> FindByIdAsync(Guid userId)
{
return UserQueryEntry.TryFetchAsync(userId);
} public Task<DpfbUser> FindByNameAsync(string userName)
{
return UserQueryEntry.TryFetchByNameAsync(userName);
} public Task UpdateAsync(DpfbUser user)
{
//throw new NotImplementedException();
return Task.FromResult();
} public void Dispose()
{
//do nothing
} public Task<DateTimeOffset> GetLockoutEndDateAsync(DpfbUser user)
{
//throw new NotImplementedException();
return Task.FromResult(new DateTimeOffset(DateTime.Now));
} public Task SetLockoutEndDateAsync(DpfbUser user, DateTimeOffset lockoutEnd)
{
//throw new NotImplementedException();
return Task.FromResult();
} public Task<int> IncrementAccessFailedCountAsync(DpfbUser user)
{
throw new NotImplementedException();
return Task.FromResult();
} public Task ResetAccessFailedCountAsync(DpfbUser user)
{
return Task.FromResult();
} public Task<int> GetAccessFailedCountAsync(DpfbUser user)
{
return Task.FromResult();
throw new NotImplementedException();
} public Task<bool> GetLockoutEnabledAsync(DpfbUser user)
{
return Task.FromResult(false);
throw new NotImplementedException();
} public Task SetLockoutEnabledAsync(DpfbUser user, bool enabled)
{
return Task.FromResult();
throw new NotImplementedException();
} public Task SetPasswordHashAsync(DpfbUser user, string passwordHash)
{
CommandBus.Send(new SetPasswordHashCommand() {UserId = user.Id, PasswordHash = passwordHash});
return Task.FromResult();
} public Task<string> GetPasswordHashAsync(DpfbUser user)
{
return UserQueryEntry.FetchPasswordHashAsync(user.Id);
} public Task<bool> HasPasswordAsync(DpfbUser user)
{
return UserQueryEntry.HasPasswordAsync(user.Id);
} public Task SetTwoFactorEnabledAsync(DpfbUser user, bool enabled)
{
return Task.FromResult(false);
throw new NotImplementedException();
} public Task<bool> GetTwoFactorEnabledAsync(DpfbUser user)
{
return Task.FromResult(false);
throw new NotImplementedException();
} public Task SetEmailAsync(DpfbUser user, string email)
{
throw new NotImplementedException();
return Task.FromResult();
} public Task<string> GetEmailAsync(DpfbUser user)
{
throw new NotImplementedException();
} public Task<bool> GetEmailConfirmedAsync(DpfbUser user)
{
throw new NotImplementedException();
return Task.FromResult(true);
} public Task SetEmailConfirmedAsync(DpfbUser user, bool confirmed)
{
throw new NotImplementedException();
return Task.FromResult();
} public Task<DpfbUser> FindByEmailAsync(string email)
{
throw new NotImplementedException();
}
}
配置
public partial class Startup
{
//配置Identity身份验证
public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext(() => new DpfbUserStore());
app.CreatePerOwinContext((IdentityFactoryOptions<AppUserManager> options,
IOwinContext context) =>
{
var manager = new AppUserManager(); //用户信息验证
manager.UserValidator = new UserValidator<DpfbUser, Guid>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = false
}; //密码验证
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = ,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
}; //配置最大出错次数
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes();
manager.MaxFailedAccessAttemptsBeforeLockout = ; //开启两步验证
manager.RegisterTwoFactorProvider("PhoneCode", new PhoneNumberTokenProvider<DpfbUser, Guid>
{
MessageFormat = "Your security code is: {0}"
});
manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<DpfbUser, Guid>
{
Subject = "SecurityCode",
BodyFormat = "Your security code is {0}"
}); //配置消息服务
manager.EmailService = new EmailService();
manager.SmsService = new SmsService(); var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<DpfbUser, Guid>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
});
app.CreatePerOwinContext(()=>new AppSignInManager()); //配置Cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/system/login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<AppUserManager, DpfbUser, Guid>(
TimeSpan.FromMinutes(),
(AppUserManager manager, DpfbUser user) =>
manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie),
user => new Guid(user.GetUserId<string>()))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); // Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes()); // Enables the application to remember the second login verification factor such as phone or email.
// Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
// This is similar to the RememberMe option when you log in.
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}
}
修改密码
AppUserManager的基类有个属性RequireUniqueEmail,当这个属性被置为true的时候,修改密码(以及其他敏感操作)会要求Email验证,对于内部系统而言,可以将这个属性置为false。
...
【加功能的时候再补充】
CQRS学习——集成ASP.NET Identity[其五]的更多相关文章
- 24.集成ASP.NETCore Identity
正常的情况下view页面的错误的显示应该是这么去判断的 这里我们就不加判断为了,直接用这个div 显示就可以了.当有错误会自动显示在div内 asp.net core Identity加入进来 这里用 ...
- CQRS学习——Dpfb以及其他[引]
[Dpfb的起名源自:Ddd Project For Beginer,这个Beginer自然就是博主我自己了.请大家在知晓这是一个入门项目的事实上,怀着对入门者表示理解的心情阅读本系列.不胜感激.] ...
- [ASP.NET MVC] ASP.NET Identity学习笔记 - 原始码下载、ID型别差异
[ASP.NET MVC] ASP.NET Identity学习笔记 - 原始码下载.ID型别差异 原始码下载 ASP.NET Identity是微软所贡献的开源项目,用来提供ASP.NET的验证.授 ...
- ASP.NET Identity 2集成到MVC5项目--笔记01
Identiry2是微软推出的Identity的升级版本,较之上一个版本更加易于扩展,总之更好用.如果需要具体细节.网上具体参考Identity2源代码下载 参考文章 在项目中,是不太想直接把这一堆堆 ...
- ASP.NET Identity 2集成到MVC5项目--笔记02
ASP.NET Identity 2集成到MVC5项目--笔记01 ASP.NET Identity 2集成到MVC5项目--笔记02 继上一篇,本篇主要是实现邮件.用户名登陆和登陆前邮件认证. 1. ...
- 从零搭建一个IdentityServer——集成Asp.net core Identity
前面的文章使用Asp.net core 5.0以及IdentityServer4搭建了一个基础的验证服务器,并实现了基于客户端证书的Oauth2.0授权流程,以及通过access token访问被保护 ...
- 学习asp.net Identity 心得体会(连接oracle)
asp.net Identity具体功能暂不在此细说,下面主要介绍几点连接oracle注意的事项, 1.首先下载连接oracle驱动Oracle.ManagedDataAccess.dll和Oracl ...
- ASP.NET Identity & OWIN 学习资料
有关 ASP.NET Identity 的更多细节: http://www.asp.net/identity 从一个空项目中添加 ASP.NET Identity 和 OWIN 支持: http:// ...
- asp.net identity的学习记录
# identity数据库 ## 创建空数据库 交给ef管理 ### 添加asp.net identity包 ``` Install-Package Microsoft.AspNet.Identity ...
随机推荐
- 如何使用javascript书写递归函数
递归函数大家都应该比较熟吧?那么,如何在javascript中书写一个完美的递归函数呢?且听我娓娓道来. 递归函数 写的时候,查了一下维基百科对递归函数的定义,恕我愚钝,简直太深奥了!所以,我还是简单 ...
- 什么是MVC开发模式以及它和传统开发模式的区别
Model1模式:使用纯JSP或者JSP+JavaBean开发,存在如下缺陷:JSP页面中混合了HTML和JAVA代码,从而给代码的开发和阅读带 来了麻烦:系统后期维护和扩展非常困难,例如在JSP页面 ...
- HttpClient(4.3.5) - HTTP Header
An HTTP message can contain a number of headers describing properties of the message such as the con ...
- 【Android】日常开发android.jar文件中十五个重要的包概述
简述Android项目中android.jar文件里包含较为重要的15个系统编译后的class文件 android.app:提供高层的程序模型,提供基本的运行环境android.content:包含各 ...
- ASP.NET下运用Memcached
对于大型网站的高并发,在ASP.NET网站下的session性能并不高,所以造成人们一种印象,大型WEB项目使用JAVA的错觉,致使很多人吐槽微 软不给力,其实这好比拉不出怪地球引力,本文介绍Memc ...
- 服务器发布WebService返回DataTable
初始化Datatable时,需要为Datatable命名.否则在客户端使用时,会报“datatable不能序列化...”导致表格无法从服务器端读取到. 例如: 服务器端: DataTable dt = ...
- 结构型模式——Adapter
1.意图 将一个类的接口转换成客户希望的另一个接口.使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 2.结构 类适配器 对象适配器 3.参与者 Target定义Client使用的与特定领域 ...
- Linux rar
http://www.vpsyou.com/2010/06/15/to-extract-rar-centos.html wget http://www.rarsoft.com/rar/rarlinux ...
- Chrome插件:网页截图
截图(Webpage Screenshot)是一款Chrome浏览器中的截图插件,使用它可以快速地截取网页中的全部内容. 这是介绍地址:http://chromecj.com/blogging/201 ...
- CorelDRAW 二维码插件
随着智能手机的流行,二维码在各个领域大量应用,这个插件在补CorelDRAW这方面的不足: 这个插件是 cpg 格式,安装请看这篇博客:http://www.cnblogs.com/o594cql/p ...