ASP.NET 5中使用AzureAD实现单点登录

 更新时间:2015年07月17日 14:49:37   投稿:hebedich  
本文给大家介绍的是在ASP.NET 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 Auth
services.Configure<ExternalAuthenticationOptions>(options =>
{
  options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});

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

// Configure the OWIN Pipeline to use Cookie Authentication
app.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 Authentication
app.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);
}

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

【更新:2015-07-16】
如果你遇到添加了 [Authorize] ,但是不能自动转到登录页面的情况,那么需要:

app.UseOpenIdConnectAuthentication(options => {
  options.AutomaticAuthentication = true;
});

具体见:https://github.com/aspnet/Security/issues/357#issuecomment-120834369

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关文章

  • .Net RabbitMQ实现HTTP API接口调用

    .Net RabbitMQ实现HTTP API接口调用

    RabbitMQ Management插件还提供了基于RESTful风格的HTTP API接口来方便调用。本文就主要介绍了.Net RabbitMQ实现HTTP API接口调用,感兴趣的可以了解一下
    2021-06-06
  • 在.Net中使用MongoDB的方法教程

    在.Net中使用MongoDB的方法教程

    最近在研究mongodb,在网上搜索发现针对.net 中使用mongodb的文章要么是早期的驱动版本,要么资料很少,所以写个随笔记录一下,本文详细的给大家介绍了在.Net中使用MongoDB的方法教程,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-05-05
  • asp.net中获取远程网页的内容之一(downmoon原创)

    asp.net中获取远程网页的内容之一(downmoon原创)

    asp.net中获取远程网页的内容之一(downmoon原创)...
    2007-04-04
  • ubuntu16.4下用jexus部署ASP.NET Core环境

    ubuntu16.4下用jexus部署ASP.NET Core环境

    这篇文章主要以图文结合的方式介绍了ubuntu16.4下ASP.NET Core部署环境搭建步骤,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • ASP.NET 后台登录小技巧介绍

    ASP.NET 后台登录小技巧介绍

    对于后台一些需要登录才能访问的页面,你是不是每次都去判断一下登录时保存在session或者cookie里面的值是否存在啊!
    2012-11-11
  • 拦截asp.net输出流并进行处理的方法

    拦截asp.net输出流并进行处理的方法

    这篇文章主要介绍了拦截asp.net输出流并进行处理的方法,比较有实用性的一个技巧,需要的朋友可以参考下
    2014-08-08
  • Visual Studio 2017 离线安装教程

    Visual Studio 2017 离线安装教程

    这篇文章主要为大家详细介绍了Visual Studio 2017 离线安装教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • asp.net+ajax的Post请求实例

    asp.net+ajax的Post请求实例

    这篇文章主要介绍了asp.net+ajax的Post请求实现方法,实例分析了Ajax的发送post数据的原理与技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-01-01
  • ASP.NET MVC实现多选下拉框

    ASP.NET MVC实现多选下拉框

    这篇文章介绍了ASP.NET MVC实现多选下拉框的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • Asp.net 页面调用javascript变量的值

    Asp.net 页面调用javascript变量的值

    开发过程中碰到了这种情况,我想将javascript中定义的变量赋值给页面中的TextBox控件.
    2009-12-12

最新评论