久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

ASP.NET Core 使用 Azure Active Directory 進(jìn)行身份驗(yàn)證并

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進(jìn)行身份驗(yàn)證并跨請求保留自定義聲明)
本文介紹了ASP.NET Core 使用 Azure Active Directory 進(jìn)行身份驗(yàn)證并跨請求保留自定義聲明的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

我在 Visual Studio 2017 中創(chuàng)建了一個(gè)默認(rèn)的 ASP.NET Core 網(wǎng)站.我選擇使用 Azure Active Directory 進(jìn)行身份驗(yàn)證.我運(yùn)行該站點(diǎn),并且可以使用 Active Directory 中的帳戶成功登錄.

I have a default ASP.NET Core website created within Visual Studio 2017. I have chosen to authenticate using an Azure Active Directory. I run the site and can successfully login using an account in the Active Directory.

我可以檢索 Active Directory 提供的聲明信息,例如通過調(diào)用以下行,我得到了名稱".

I can retrieve Claim information provided by Active Directory, e.g. by calling the following line I get the 'name'.

User.Claims.FirstOrDefault(c => c.Type == "name")?.Value;

我想為登錄用戶添加自定義聲明 - CompanyId = 123456.我可以添加自定義聲明,但它僅在設(shè)置聲明的頁面上可用.

I want to add a custom claim - CompanyId = 123456 for the logged in user. I'm able to add a custom claim however it is only available on the page where the claim is set.

Claim claim = new Claim("CompanyId", "123456", ClaimValueTypes.String);
((ClaimsIdentity)User.Identity).AddClaim(claim);

我的理解是,我需要以某種方式更新由 Active Directory 頒發(fā)的令牌或在頒發(fā)令牌之前設(shè)置聲明.我不確定該怎么做.

My understanding is that I somehow need to update the token that has been issued by Active Directory or set the claim before the token is issued. I'm unsure how to do this.

我懷疑這需要在 SignIn() 的 AccountController 中完成

I suspect this needs to be done in the AccountController at SignIn()

// GET: /Account/SignIn
[HttpGet]
public IActionResult SignIn()
{
    return Challenge(
            new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectDefaults.AuthenticationScheme);
}

我已經(jīng)閱讀了許多關(guān)于此場景的文章和示例(包括 https://github.com/ahelland/AADGuide-CodeSamples/tree/master/ClaimsWebApp)但是沒有設(shè)法解決如何跨請求持久化聲明.

I've read numerous articles and samples about this scenario (including https://github.com/ahelland/AADGuide-CodeSamples/tree/master/ClaimsWebApp) however have not managed to solve how to persist the Claim across requests.

我已經(jīng)成功地使用 ASP.NET 身份作為身份驗(yàn)證提供程序來持久化自定義聲明,但這似乎是因?yàn)樽远x聲明已保存到數(shù)據(jù)庫中...

I have successfully managed to persist custom Claims using ASP.NET Identity as the Authentication Provider, but this appears to be because the custom Claim is saved to the database..

推薦答案

OnTokenValidated 為您提供了修改從傳入令牌獲得的 ClaimsIdentity 的機(jī)會(huì),下面的代碼用于您的參考:

OnTokenValidated offers you the chance to modify the ClaimsIdentity obtained from the incoming token , code below is for your reference :

private Task TokenValidated(TokenValidatedContext context)
{
    Claim claim = new Claim("CompanyId", "123456", ClaimValueTypes.String);
    (context.Ticket.Principal.Identity as ClaimsIdentity).AddClaim(claim);

    return Task.FromResult(0);
}

設(shè)置OpenIdConnectEvents:

Events = new OpenIdConnectEvents
{
    OnRemoteFailure = OnAuthenticationFailed,
    OnAuthorizationCodeReceived = OnAuthorizationCodeReceived,

    OnTokenValidated = TokenValidated
}

然后在控制器中使用:

var companyId=  User.Claims.FirstOrDefault(c => c.Type == "CompanyId")?.Value;

這篇關(guān)于ASP.NET Core 使用 Azure Active Directory 進(jìn)行身份驗(yàn)證并跨請求保留自定義聲明的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權(quán)不起作用)
ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登錄使用角色)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護(hù)進(jìn)程或服務(wù)器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發(fā)技
.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 獲取 AAD 訪問令牌以與 Microsoft Graph 一起使用)
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調(diào)用時(shí) Azure KeyVault Active Directory AcquireTokenAsync 超時(shí))
Getting access token using email address and app password from oauth2/token(使用電子郵件地址和應(yīng)用程序密碼從 oauth2/token 獲取訪問令牌)
主站蜘蛛池模板: 日韩不卡一区二区 | 在线观看亚洲专区 | 国产激情视频在线 | 亚洲 日本 欧美 中文幕 | 成人水多啪啪片 | 久久国产精品一区二区三区 | 91久久精品国产91久久性色tv | 一区二区三区免费在线观看 | 天堂在线www | 欧美国产日韩在线观看 | 亚洲伦理自拍 | 欧洲av一区| 国产日韩欧美在线观看 | 欧美日韩在线精品 | av午夜电影 | 在线三级网址 | 91国内精品久久 | av国产在线观看 | 国产精品福利在线 | 美女天天操 | 国产成人在线免费 | 狠狠躁夜夜躁人人爽天天高潮 | 久久伦理中文字幕 | 亚洲精久久久 | 久在线 | 成人在线精品 | 亚洲国产精品自拍 | 欧美日韩在线电影 | 97精品久久 | 鲁视频| 婷婷久久网 | 亚洲一区二区中文字幕在线观看 | 最新日韩在线视频 | 免费黄色片在线观看 | 亚洲福利网 | 国产麻豆一区二区三区 | 亚洲成人一区二区 | 狠狠色综合久久婷婷 | 91麻豆精品国产91久久久久久久久 | 久草在线青青草 | 国产精品亚洲第一区在线暖暖韩国 |