[ASP.NET MVC] ASP.NET Identity登入技术应用
[ASP.NET MVC] ASP.NET Identity登入技术应用
情景
ASP.NET Identity是微软所贡献的开源项目,用来提供ASP.NET的验证、授权等等机制。在ASP.NET Identity里除了提供最基础的:用户注册、密码重设、密码验证等等基础功能之外,也提供了进阶的:Cookie登入、Facebook登入、Google登入等等进阶功能。套用这些功能模块,让开发人员可以快速的在ASP.NET站台上,提供验证、授权等等机制。
但是在企业中,开发人员常常会遇到一种开发情景就是:企业里已经有一套既有身分系统,这个系统提供了:用户注册、密码重设、密码验证等等功能,新开发的ASP.NET站台,必须要串接既有身分系统来提供验证、授权等等机制。这个既有身分系统,可能是大卖场会员管理系统、也可能是银行帐户管理系统,它们的注册审核机制有一套严谨并且固定的流程。
在这样的开发情景中,开发人员可能会选择透过程序接口、OAuth机制等等方式,将既有身分系统整合成为ASP.NET Identity的验证提供者。这样两个系统之间的整合,除了有一定的高技术门坎之外。在整合之后,两个系统之间互相重迭的功能模块,操作流程的冲突该如何处理,也是一个需要额外考虑的复杂问题。
一个好消息是,ASP.NET Identity拥有高度模块化的软件架构。在ASP.NET Identity中,将Cookie登入、Facebook登入、Google登入等等功能模块,切割为独立的ASP.NET Security套件。开发人员完全可以直接套用ASP.NET Security套件,快速整合既有的身分系统,就可以提供ASP.NET站台所需的验证、授权等等机制。本篇文章介绍如何套用ASP.NET Security来整合既有身分系统,用以提供ASP.NET站台所需的验证、授权等等机制。主要为自己留个纪录,也希望能帮助到有需要的开发人员。
范例
范例程序代码:下载地址
开发
开始套用ASP.NET Security之前,先建立一个空白的MVC项目,来提供一个新的ASP.NET站台。并且变更预设的Web服务器URL为:「http://localhost:41532/」,方便完成后续的开发步骤。
再来在MVC项目里加入三个ASP.NET Security的NuGet套件参考:Microsoft.AspNet.Authentication、Microsoft.AspNet.Authentication.Cookies、Microsoft.AspNet.Authentication.Facebook。
接着建立AccountController以及相关的View,用以提供登入页面,让使用者可以选择使用哪种模式登入系统。
public class AccountController : Controller
{
// Methods
public IActionResult Login(string returnUrl = null)
{
// ViewData
this.ViewData["ReturnUrl"] = returnUrl;
// Return
return View();
}
}
再来在MVC项目内加入下列程序代码,用以挂载与设定后续要使用的两个CookieAuthenticationMiddleware。(关于程序代码的相关背景知识,请参阅技术剖析说明:ASP.NET Identity登入技术剖析)
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Authentication
services.AddAuthentication(options =>
{
options.SignInScheme = IdentityOptions.Current.ExternalCookieAuthenticationScheme;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Authentication
app.UseCookieAuthentication(options =>
{
options.AuthenticationScheme = IdentityOptions.Current.ApplicationCookieAuthenticationScheme;
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
options.LoginPath = new PathString("/Account/login");
});
app.UseCookieAuthentication(options =>
{
options.AuthenticationScheme = IdentityOptions.Current.ExternalCookieAuthenticationScheme;
options.AutomaticAuthenticate = false;
options.AutomaticChallenge = false;
options.LoginPath = null;
});
}
}
最后在MVC项目内,建立ExistingIdentitySystem这个类别用来仿真既有身分系统。为了方便理解系统,ExistingIdentitySystem里的PasswordSignIn(密码登入)、ExternalSignIn(第三方登入ex:FB登入)等方法都直接回传成功讯息,而GetUserById(取得使用者)这个方法则是直接回传固定的用户信息。(在正式环境开发时,上述方法可以实作为透过WebAPI、或是直接连通数据库等等方式,与既有身分系统取得相关信息。)
public class ExistingIdentitySystem
{
// Methods
public ExistingUser GetUserById(string userId)
{
// Result
var user = new ExistingUser();
user.Id = "Clark.Lab@hotmail.com";
user.Name = "Clark";
user.Birthday = DateTime.Now;
// Return
return user;
}
public bool PasswordSignIn(string userId, string password)
{
// Return
return true;
}
public bool ExternalSignIn(string userId, string externalProvider)
{
switch (externalProvider)
{
case "Facebook": return true;
default:
return true;
}
}
}
public class ExistingUser
{
// Properties
public string Id { get; set; }
public string Name { get; set; }
public DateTime Birthday { get; set; }
}
开发 - Facebook Authentication
完成上述步骤后,接着着手开发Facebook验证。首先开发人员可以到Facebook开发者中心(https://developers.facebook.com/),注册一个新的APP账号。(测试用的Site URL为先前步骤定义的:「http://localhost:41532/」)
接着在MVC项目内加入下列程序代码,用以挂载与设定FacebookAuthenticationMiddleware。在这其中AppId、AppSecret是Facebook开发者中心提供的APP账号数据,而Scope、UserInformationEndpoint两个参数则是定义要额外取得用户的E-Mail信息。
public class Startup
{
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Authentication
app.UseFacebookAuthentication(options =>
{
options.AppId = "770764239696406";
options.AppSecret = "2eecc0b9ef785e43bcd4779e2803ba0f";
options.Scope.Add("email");
options.UserInformationEndpoint = "https://graph.facebook.com/v2.5/me?fields=id,name,email";
});
}
}
再来打开AccountController加入下列程序代码以及对应的View,用以提供ASP.NET站台处理Facebook这类的第三方登入(ExternalLogin)。在这其中,ExternalLogin用来发起一个验证挑战(Challenge),系统会依照externalProvider参数,来决定是要向Facebook或是其他第三方系统做验证。
当用户通过验证后,系统会调用ExternalLoginCallback来处理验证结果。在ExternalLoginCallback里会取得验证结果中FBUser的UserId,用来与ExistingIdentitySystem做验证。如果验证通过,会接着从ExistingIdentitySystem取得对应的ExistingUser、再转换为APPUser来真正登入系统。(关于程序代码的相关背景知识,请参阅技术剖析说明:ASP.NET Identity登入技术剖析)
public class AccountController : Controller
{
public IActionResult ExternalLogin(string externalProvider, string returnUrl = null)
{
// AuthenticationProperties
var authenticationProperties = new AuthenticationProperties();
authenticationProperties.Items.Add("ExternalProvider", externalProvider);
authenticationProperties.RedirectUri = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl });
// Return
return new ChallengeResult(externalProvider, authenticationProperties);
}
public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null)
{
// AuthenticateContext
var authenticateContext = new AuthenticateContext(IdentityOptions.Current.ExternalCookieAuthenticationScheme);
await this.HttpContext.Authentication.AuthenticateAsync(authenticateContext);
// AuthenticateInfo
string userId = authenticateContext.Principal.FindFirst(ClaimTypes.Email).Value;
string externalProvider = authenticateContext.Properties["ExternalProvider"] as string;
// Login
var existingIdentitySystem = new ExistingIdentitySystem();
if (existingIdentitySystem.ExternalSignIn(userId, externalProvider) == false)
{
throw new InvalidOperationException();
}
// ExistingUser
var existingUser = existingIdentitySystem.GetUserById(userId);
if (existingUser == null) throw new InvalidOperationException();
// ApplicationUser
var applicationIdentity = new ClaimsIdentity(IdentityOptions.Current.ApplicationCookieAuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
applicationIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, existingUser.Id));
applicationIdentity.AddClaim(new Claim(ClaimTypes.Name, existingUser.Name));
var applicationUser = new ClaimsPrincipal(applicationIdentity);
// Cookie
await this.HttpContext.Authentication.SignInAsync(IdentityOptions.Current.ApplicationCookieAuthenticationScheme, applicationUser);
await this.HttpContext.Authentication.SignOutAsync(IdentityOptions.Current.ExternalCookieAuthenticationScheme);
// Return
return Redirect(returnUrl);
}
}
开发 - Password Authentication
完成上述步骤后,接着着手开发Password验证。打开AccountController加入下列程序代码以及对应的View,用以提供ASP.NET站台处理Password验证。在这其中,PasswordLogin会接收用户输入的账号密码,用来与ExistingIdentitySystem做验证。如果验证通过,会接着从ExistingIdentitySystem取得ExistingUser、再转换为APPUser来真正登入系统。(关于程序代码的相关背景知识,请参阅技术剖析说明:ASP.NET Identity登入技术剖析)
public class AccountController : Controller
{
public async Task<IActionResult> PasswordLogin(string userId, string password, string returnUrl = null)
{
// Login
var existingIdentitySystem = new ExistingIdentitySystem();
if (existingIdentitySystem.PasswordSignIn(userId, password) == false)
{
throw new InvalidOperationException();
}
// ExistingUser
var existingUser = existingIdentitySystem.GetUserById(userId);
if (existingUser == null) throw new InvalidOperationException();
// ApplicationUser
var applicationIdentity = new ClaimsIdentity(IdentityOptions.Current.ApplicationCookieAuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
applicationIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, existingUser.Id));
applicationIdentity.AddClaim(new Claim(ClaimTypes.Name, existingUser.Name));
var applicationUser = new ClaimsPrincipal(applicationIdentity);
// Cookie
await this.HttpContext.Authentication.SignInAsync(IdentityOptions.Current.ApplicationCookieAuthenticationScheme, applicationUser);
await this.HttpContext.Authentication.SignOutAsync(IdentityOptions.Current.ExternalCookieAuthenticationScheme);
// Return
return Redirect(returnUrl);
}
}
使用
完成开发步骤后,当系统执行到打上[Authorize]标签的Controller或是Action时,就会跳转到Login页面。
public class HomeController : Controller
{
[Authorize]
public IActionResult Contact()
{
ViewData["Message"] = "Hello " + User.Identity.Name + "!";
return View();
}
}
使用 - Facebook Authentication
在Login页面,当使用者选择使用Facebook验证,系统会跳转到Facebook页面进行验证与授权。完成验证授权的相关步骤后,使用者就可以进入被打上[Authorize]标签的Controller或是Action。
使用 - Password Authentication
在Login页面,当使用者选择使用Password验证,系统会使用Login页面上输入的账号密码来进行验证与授权。完成验证授权的相关步骤后,使用者就可以进入被打上[Authorize]标签的Controller或是Action。
范例
范例程序代码:下载地址
[ASP.NET MVC] ASP.NET Identity登入技术应用的更多相关文章
- [ASP.NET MVC] ASP.NET Identity登入技术剖析
[ASP.NET MVC] ASP.NET Identity登入技术剖析 前言 ASP.NET Identity是微软所贡献的开源项目,用来提供ASP.NET的验证.授权等等机制.本篇文章介绍ASP. ...
- [ASP.NET MVC] ASP.NET Identity学习笔记 - 原始码下载、ID型别差异
[ASP.NET MVC] ASP.NET Identity学习笔记 - 原始码下载.ID型别差异 原始码下载 ASP.NET Identity是微软所贡献的开源项目,用来提供ASP.NET的验证.授 ...
- [Asp.net MVC]Asp.net MVC5系列——添加视图
目录 系列文章 概述 添加视图 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 概述 在这一部分我们添加一个新的控制器HelloWorldController类, ...
- [Asp.net MVC]Asp.net MVC5系列——在模型中添加验证规则
目录 概述 在模型中添加验证规则 自定义验证规则 伙伴类的使用 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5 ...
- [Asp.net MVC]Asp.net MVC5系列——添加模型
目录 概述 添加模型 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列——添加视图 概述 在本节中我们将追加 ...
- [Asp.net MVC]Asp.net MVC5系列——从控制器访问模型中的数据
目录 概述 从控制器访问模型中的数据 强类型模型与@model关键字 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net M ...
- [Asp.net MVC]Asp.net MVC5系列——添加数据
目录 概述 显示添加数据时所用表单 处理HTTP-POST 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列 ...
- [Asp.net MVC]Asp.net MVC5系列——布局视图
目录 系列文章 概述 布局视图 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列——添加视图 [Asp.net M ...
- Asp.net MVC]Asp.net MVC5系列——Routing特性
目录 概述 路由特性 使用路由 可选参数和参数的默认值 路由前缀 默认路由 路由约束 自定义路由约束 路由名 区域(Area) 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列— ...
随机推荐
- GUID简介
GUID (全局唯一标识符) 编辑 全局唯一标识符(GUID,Globally Unique Identifier)是一种由算法生成的二进制长度为128位的数字标识符.GUID主要用于在拥有多个节点. ...
- String,StringBuffer与StringBuilder的区别??
转自http://blog.csdn.net/rmn190/article/details/1492013 String 字符串常量 StringBuffer 字符串变量(线程安全) StringBu ...
- Windws Server 2008 R2 WEB环境配置之MYSQL 5.6.22安装配置
版本选择 因为MySql的版本越来越多,而作为中小网站者可能没有足够的经济去购买商业版本,所以一般选择免费版,而且功能也是足够使用的. 有钱任性就下载企业版,哈哈. 目前使用最多的版本是mysql i ...
- 编写简单的ramdisk(有请求队列)
前言 前面用无请求队列实现的ramdisk的驱动程序虽然申请了请求队列,但实际上没用上,因为ramdisk不像实际的磁盘访问速度慢需要缓存,ramdisk之间使用内存空间,所以就没用请求队列了.本文将 ...
- vc下打印透明背景图片
一.前言 刚接到个任务,要把带有透明背景的章子图片打印出来,开始觉得不是很简单吗,直接用vc自动生成的打印功能不就ok了.不过问题却不是想像的那么简单! 二.窗口中显示透明图片 在窗口中显示图片,可以 ...
- 移动前端开发-单页应用(spa)模型
一门新的技术诞生总会引来一番争议,单页Web应用程序也不例外,其最大的优势在于用户体验,对于内容的改动不需要加载整个页面:对服务器压力很小,消耗更少的带宽,与面向服务的架构更好地结合.使用HTML+C ...
- npm上传自己的项目
npm安装就不介绍了,自行度娘.本文介绍npm上传 先初始化:npm init 根据提示填完系统介绍信息(package.json): 再登录npmjs: npm login 效果如图: 输入注册的用 ...
- android 之 ListView 里面嵌套 GridView 遇到的问题及其解决方法。
我们直接入主题.所有问题例子请参照下图 1,怎样使图片具有点击事件? 答: 解决方法: 在你的BaseAdapter里面不要设置下面这三个东西,然后再设置GridView的onItemClick. g ...
- 用CSS3动画,让页面动起来
以前就听说过有个库,叫animate.css,但是自己并没有在实际项目中使用过,这次正好要做个招聘页面,得以利用一下这个库,在经常会卡顿的UC浏览器中也能流畅执行. 扫描下面的二维码,可以看到在线的d ...
- 重温Servlet学习笔记--servletContext对象
一个项目中只有一个ServletContext对象,我们可以在多个servlet中获取这个唯一的对象,使用它可以给多个servlet传递数据,我们通常成servletContext为上下文对象.这个对 ...