当前位置: 首页 > 编程笔记 >

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

高展
2023-03-14
本文向大家介绍ASP.NET 5中使用AzureAD实现单点登录,包括了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

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

 类似资料:
  • 问题内容: 我想使用Django为我们当前使用的多个应用程序实现单一登录(SSO)。如何使用Django实现SSO?是否有可用于实施SSO的Django软件包? 请帮忙谢谢 问题答案: 我们正在使用OpenAM。 http://forgerock.com/openam.html OpenAM Cookie表示用户已通过身份验证。 为此的身份验证后端非常简单。少于50行代码。 https://doc

  • 对于Spring MVC应用程序,通常如何使用SAML 2.0实现SSO? 我的应用程序需要实现SSO,以便用户无需使用我的应用程序创建新帐户即可登录。 我的理解是,如果我错了,请纠正我,我需要一个服务提供商与第三方使用的身份提供商进行通信,以便交换元数据。但我该如何实现这一过程呢? 此外,Spring MVC应用程序端需要什么? 提前感谢:D

  • 我的任务是为我们的客户实施单点登录,作为我们下一个版本的一部分。流程如下: 用户使用学校提供给他/她的学生ID/密码登录学校的主要门户系统。 用户点击我公司产品的链接。 用户会自动进入仪表板页面,就好像他们刚刚通过我们网站上的登录表单登录一样。 因此,有两种机制可以将用户验证到我们的站点: 访问我们产品的主页,并使用我们存储在本地系统中的电子邮件/密码登录。 使用单点登录,学生已经使用学生id和密

  • 主要内容:1.父域 Cookie,2.认证中心,3.LocalStorage 跨域单点登录在现在的系统架构中广泛存在,他将多个子系统的认证体系打通,实现了一个入口多处使用,而在架构单点登录时,也会遇到一些小问题,在不同的应用环境中可以采用不同的单点登录实现方案来满足需求。 1.父域 Cookie 在将具体实现之前,我们先来聊一聊 Cookie 的作用域。 Cookie 的作用域由 domain 属性和 path 属性共同决定。domain 属性的有效值为当前域或其父域的域名/I

  • 我正在尝试通过SAML请求将SSO集成到我们的ASP. NET应用程序中。我正在使用KentorAuthService库来实现这一点。使用kentor库是否是从其他身份提供者(例如(一次登录、快速身份等)进行身份验证的解决方案,或者我应该专门基于身份提供者实施。

  • 本文向大家介绍asp.net简单实现单点登录(SSO)的方法,包括了asp.net简单实现单点登录(SSO)的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了asp.net简单实现单点登录(SSO)的方法。分享给大家供大家参考,具体如下: 单点登录(Single Sign On , 简称 SSO )是目前比较流行的服务于企业业务整合的解决方案之一, SSO 使得在多个应用系统中,用户只

  • 本文向大家介绍golang实现单点登录系统(go-sso),包括了golang实现单点登录系统(go-sso)的使用技巧和注意事项,需要的朋友参考一下 这是一个基于Go语言开发的单点登录系统,实现手机号注册、手机号+验证码登录、手机号+密码登录、账号登出等功能,用户认证采用cookie和jwt两种方式。收发短信相关方法已提供,仅需根据短信通道提供商提供的接口做相应的参数配置即可使用。 环境介绍 g

  • 作者:陈希章 发表于 2017年12月27日 这篇文章经过多次修改,终于在今天晚上写完了,演示用的范例代码也终于跑通了。因为这个SSO的功能目前只是Preview的状态,所以本篇文章严格参考了官方的文档,并且对其中的重点环节做了提示,对最终效果做了说明。 官方的文档请参考 https://docs.microsoft.com/en-us/office/dev/add-ins/develop/sso