ASP.NET 5探险(5):利用AzureAD实现单点登录

题记:在ASP.NET 5中虽然继续可以沿用ASP.NET Identity来做验证授权,不过也可以很容易集成支持标准协议的第三方服务,比如Azure Active Directory。

其实,在ASP.NET 5中集成AzureAD,利用其进行验证和授权,是非常简单的。因为:首先Azure Active Directory提供了OAuth2.0、OpenId Connect 1.0、SAML和WS-Federation 1.2标准协议接口;其次微软在ASP.NET 5中移植了集成OpenId Connect的OWIN中间件。所以,只要在ASP.NET 5项目中引用"Microsoft.AspNet.Authentication.OpenIdConnect"这个包,并正确配置AzureAD的连接信息,就可以很容易的进行集成。

大致步骤如下:

1,在config.json文件中添加AzureAD的配置信息:

"AzureAd": {    "ClientId": "[Enter the clientId of your application as obtained from portal, e.g. ba74781c2-53c2-442a-97c2-3d60re42f403]",    "Tenant": "[Enter the name of your tenant, e.g. contoso.onmicrosoft.com]",    "AadInstance": "https://login.microsoftonline.com/{0}", // This is the public instance of Azure AD    "PostLogoutRedirectUri":  https://localhost:44322/}

2,修改project.json,引入OpenIdConnect的中间件:

"Microsoft.AspNet.Authentication.OpenIdConnect": "1.0.0-*"

3,在Startup中的ConfigureServices方法里面添加:

// OpenID Connect Authentication Requires Cookie Authservices.Configure<ExternalAuthenticationOptions>(options =>{    options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;});

4,在Startup中的Configure方法里面添加:

// Configure the OWIN Pipeline to use Cookie Authenticationapp.UseCookieAuthentication(options => {    // By default, all middleware are passive/not automatic. Making cookie middleware automatic so that it acts on all the messages.    options.AutomaticAuthentication = true;

});

// Configure the OWIN Pipeline to use OpenId Connect Authenticationapp.UseOpenIdConnectAuthentication(options =>{    options.ClientId = Configuration.Get("AzureAd:ClientId");    options.Authority = String.Format(Configuration.Get("AzureAd:AadInstance"), Configuration.Get("AzureAd:Tenant"));    options.PostLogoutRedirectUri = Configuration.Get("AzureAd:PostLogoutRedirectUri");    options.Notifications = new OpenIdConnectAuthenticationNotifications    {        AuthenticationFailed = OnAuthenticationFailed,    };});

5,Startup的OnAuthenticationFailed方法为:

private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification){    notification.HandleResponse();    notification.Response.Redirect("/Home/Error?message=" + notification.Exception.Message);    return Task.FromResult(0);}

.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; }

6,添加一个名为AccountController的Controller:

public class AccountController : Controller{    // GET: /Account/Login    [HttpGet]    public IActionResult Login()    {        if (Context.User == null || !Context.User.Identity.IsAuthenticated)            return new ChallengeResult(OpenIdConnectAuthenticationDefaults.AuthenticationScheme, new AuthenticationProperties { RedirectUri = "/" });        return RedirectToAction("Index", "Home");    }

    // GET: /Account/LogOff    [HttpGet]    public IActionResult LogOff()    {        if (Context.User.Identity.IsAuthenticated)        {            Context.Authentication.SignOut(CookieAuthenticationDefaults.AuthenticationScheme);            Context.Authentication.SignOut(OpenIdConnectAuthenticationDefaults.AuthenticationScheme);        }        return RedirectToAction("Index", "Home");    }}

以上代码也可以到我Fork的完整示例项目中找到:https://github.com/heavenwing/WebApp-OpenIdConnect-AspNet5

时间: 2024-08-25 00:42:21

ASP.NET 5探险(5):利用AzureAD实现单点登录的相关文章

ASP.net 资源请求漏洞利用工具PadBuster

ASP.net 资源请求漏洞利用工具PadBuster 在ASP.net 网站中,为了便于部署网站项目,开发者往往会将资源(图片.Javascript文件)嵌入到dll文件中.而网页中,会使用WebResource.axd?d=XXX的形式请求资源.其中,XXX采用CBC-R加密的方式生成的访问密钥.由于CBC-R算法存在Padding Oracle漏洞,所以导致渗透人员可以非法访问网站敏感文件,如web.config.PadBuster是Kali Linux提供的一款专向工具.该工具使用Per

ASP.NET 5探险(3):使用UMEditor并实现图片上传

(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:今天将继续上一篇来讲解百度富文本Web编辑器UEditor或UMEditor的使用. 上一篇"ASP.NET 5探险",我给大家分享了如何在ASP.NET 5中实现文件上传.我之所以研究这一问题的原因,就是要在百度的百度富文本Web编辑器UMEditor中实现图片上传.那么今天我们回过头来看看如何在ASP.NET 5的MVC 6项目中使用UMEditor(UEditor的使

ASP.NET单点登录(代码)

由于某些原因,在我们的应用中会遇到一个用户只能在一个地方登录的情况,也就是我们通常所说的单点登录.在ASP.NET中实现单点登录其实很简单,下面就把主要的方法和全部代码进行分析.[/p][p=25, null, left]实现思路[/p][p=25, null, left]利用Cache的功能,我们把用户的登录信息保存在Cache中,并设置过期时间为Session失效的时间,因此,一旦Session失效,我们的Cache也过期:而Cache对所有的用户都可以访问,因此,用它保存用户信息比数据库来

asp.net 跨域单点登录 【转】

关键字:单点登录   跨域    跨域单点登录 源代码下载:http://download.csdn.net/source/1571879 单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. asp.net跨域单点登录分为: 1.跨子域单点登录.如 blog.a.com 和 info.a.com 这2个站点同属一个主域.a.com,实现跨子域单点登录很简单,可以

ASP.NET实现SSO单点登录

利用ASP.NET Forms身份认证轻轻松松实现单点登录 比如我们的主域名是domain.com 其他的二级域名有 1.list.domain.com 2.item.domain.com 3.home.domain.com 登录和注册都放在passport.domain.com这个二级域名下去处理 在Web.config中配置 1 <authentication mode="Forms"> 4 <forms name="CNBDQ" loginU

asp.net 真正实现完全跨域单点登录

单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. asp.net跨域单点登录分为: 1.跨子域单点登录.如 blog.a.com 和 info.a.com 这2个站点同属一个主域.a.com,实现跨子域单点登录很简单,可以利用cookie,设置Domain为”.a.com'即可,这里就不再赘叙. 2.完成跨域单点登录.如 www.a.com www.b.com

SSO单点登录之Asp.Net实现示例

一.什么是单点登录SSO(Single Sign-On) SSO是一种统一 认知 和授权机制,指访问用同一服务器不同应用中的受保护资源的同一用户,只需登录一次,即通过一个应用中的安全验证 后,再访问其他 应用中的受保护资源时,不再需要重新 登录验证. 注: 1.所有应用 系统共享一个身份认证系统. 2.所有应用系统能够 识别和提取ticket信息. 二.实现过程 及原理说明 1.Session_Server项目作为用户认证系统,其他网站也使用它作为用户认证 2.对于同一个浏览器 的同一次会员,总

利用Wireshark抓包登录博客园

1,打开WireShark,选择本地网卡. 2,在filter中输入过滤条件Http. 3,找到http包中的get 和post的包,这时你可以找到你登录的用户名和密码了,这个用户名和密码通过表单进行验证. 利用Wireshark抓包登录博客园,布布扣,bubuko.com

思道OA之ASP.NET OA系统单点登录集成技术

思道OA开发版支持第三方集成,可以第三方的各种管理系统.校园数字化系统.统一认证,进行单点集成登录. (一)第三方系统单点登录到OA系统 下载源码 步骤1: web.config添加以下代码,允许匿名用户访问/sso.aspx,默认开发版已经添加, 将sso.aspx放置到OA目录Webroot下. <location path="sso.aspx"> <system.web> <authorization> <allow users=&quo