一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - ASP.NET教程 - ASP.NET 5中使用AzureAD實(shí)現(xiàn)單點(diǎn)登錄

ASP.NET 5中使用AzureAD實(shí)現(xiàn)單點(diǎn)登錄

2019-12-18 16:16hebedich ASP.NET教程

本文給大家介紹的是在ASP.NET 5中使用AzureAD實(shí)現(xiàn)單點(diǎn)登錄的方法和示例,有需要的小伙伴可以參考下。

題記:在ASP.NET 5中雖然繼續(xù)可以沿用ASP.NET Identity來(lái)做驗(yàn)證授權(quán),不過(guò)也可以很容易集成支持標(biāo)準(zhǔn)協(xié)議的第三方服務(wù),比如Azure Active Directory。

其實(shí),在ASP.NET 5中集成AzureAD,利用其進(jìn)行驗(yàn)證和授權(quán),是非常簡(jiǎn)單的。因?yàn)椋菏紫華zure Active Directory提供了OAuth2.0、OpenId Connect 1.0、SAML和WS-Federation 1.2標(biāo)準(zhǔn)協(xié)議接口;其次微軟在ASP.NET 5中移植了集成OpenId Connect的OWIN中間件。所以,只要在ASP.NET 5項(xiàng)目中引用"Microsoft.AspNet.Authentication.OpenIdConnect"這個(gè)包,并正確配置AzureAD的連接信息,就可以很容易的進(jìn)行集成。

大致步驟如下:

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

?
1
2
3
4
5
6
"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的中間件:

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

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

?
1
2
3
4
5
// OpenID Connect Authentication Requires Cookie Auth
services.Configure<ExternalAuthenticationOptions>(options =>
{
  options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 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方法為:

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

6,添加一個(gè)名為AccountController的Controller:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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的完整示例項(xiàng)目中找到:https://github.com/heavenwing/WebApp-OpenIdConnect-AspNet5

【更新:2015-07-16】
如果你遇到添加了 [Authorize] ,但是不能自動(dòng)轉(zhuǎn)到登錄頁(yè)面的情況,那么需要:

?
1
2
3
app.UseOpenIdConnectAuthentication(options => {
  options.AutomaticAuthentication = true;
});

具體見(jiàn):https://github.com/aspnet/Security/issues/357#issuecomment-120834369

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91制片厂制作传媒免费版樱花 | 秋霞色 | 国产一区二区精品久久 | 999久久久 | 久久这里都是精品 | 天海翼黄色三级 | 国产卡一卡二卡三乱码手机 | 999精品视频这里只有精品 | 精品亚洲国产一区二区 | 紧身牛仔裤美女被啪啪久久网 | 亚洲国产成人久久综合一 | 无人在线观看免费高清视频播放 | 日本爽p大片免费观看 | 91久久青青青国产免费 | 欧美成人免费观看国产 | 亚洲国产精品嫩草影院永久 | 99re5在线精品视频热线 | 免费午夜影片在线观看影院 | 男同志与动人物zozotv | 日韩 视频在线播放 | 激情影院免费观看 | 欧美男女爱爱视频 | 日韩欧美综合在线二区三区 | 视频在线观看高清免费 | 调教开发新婚娇妻放荡 | 精品视频免费在线观看 | 日本成人黄色网址 | 男女真实无遮挡xx00动态图软件 | 亚洲成人三级 | 成人国产在线视频在线观看 | 国产1区精品 | 女人叉开腿让男人桶 | 丝瓜视频黄色在线观看 | 精品免费tv久久久久久久 | 国产精品香蕉在线观看不卡 | 国产手机在线αⅴ片无码观看 | 男人午夜剧场 | 亚洲国产精品自在自线观看 | 日本小网站 | 91交换论坛 | 九九热在线视频观看这里只有精品 |