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

ASP.NET Core 使用 Azure Active Directory 進行身份驗證并

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

問題描述

限時送ChatGPT賬號..

我在 Visual Studio 2017 中創建了一個默認的 ASP.NET Core 網站.我選擇使用 Azure Active Directory 進行身份驗證.我運行該站點,并且可以使用 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 提供的聲明信息,例如通過調用以下行,我得到了名稱".

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.我可以添加自定義聲明,但它僅在設置聲明的頁面上可用.

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 頒發的令牌或在頒發令牌之前設置聲明.我不確定該怎么做.

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

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

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.

我已經成功地使用 ASP.NET 身份作為身份驗證提供程序來持久化自定義聲明,但這似乎是因為自定義聲明已保存到數據庫中...

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 的機會,下面的代碼用于您的參考:

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

設置OpenIdConnectEvents:

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

    OnTokenValidated = TokenValidated
}

然后在控制器中使用:

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

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

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

相關文檔推薦

ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權不起作用)
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(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
.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(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
Getting access token using email address and app password from oauth2/token(使用電子郵件地址和應用程序密碼從 oauth2/token 獲取訪問令牌)
主站蜘蛛池模板: 黄色片网站免费 | 久久日av | 在线看91 | 欧美成人三级在线观看 | 久久久网站 | 午夜网址| 欧美性猛交xxxx免费看久久久 | 少妇福利视频 | 日韩不卡在线观看 | 亚洲成人精品视频 | 久久精品国产视频 | 日韩欧美一区二区在线观看 | 欧美在线视频一区二区 | 亚洲黄色天堂 | 97视频免费 | 日本成人精品 | aa久久| 黄色成人在线观看 | 国产在线一区二区三区 | 在线播放黄色 | 午夜视频免费在线观看 | 在线播放国产精品 | 黄视频免费在线观看 | 波多野结衣久久 | 色在线播放 | 亚洲欧美日韩在线 | 91午夜精品亚洲一区二区三区 | 瑟瑟视频在线观看 | 一本不卡 | 久久久综合网 | 久色91| 逼逼操| 成人在线不卡 | 国产午夜视频 | 四虎视频| 欧美在线激情 | 91视频专区 | 日韩免费小视频 | 国产成人精品亚洲男人的天堂 | 在线网站免费观看18 | 欧美视频久久 |