久久久久久久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 獲取訪問令牌)
主站蜘蛛池模板: 国产一区二区电影网 | 狠狠做深爱婷婷综合一区 | 国产美女永久免费无遮挡 | 大象视频一区二区 | 日韩欧美在线不卡 | 午夜tv免费观看 | 亚洲欧美日韩在线 | 毛片毛片毛片毛片毛片 | 中文字幕日韩欧美一区二区三区 | 欧美国产91 | 91在线看 | 九九热视频这里只有精品 | 亚州中文字幕 | 成人免费毛片片v | 另类专区成人 | 人人人人干 | 国产成人网 | 亚洲日本乱码在线观看 | 亚洲综合在线视频 | 麻豆一区一区三区四区 | 国产成人免费在线 | 欧美国产在线一区 | 国产精品国产三级国产aⅴ无密码 | 久草新在线 | 91精品国产乱码久久久 | 亚洲国产网址 | 极品销魂美女一区二区 | 国产成人综合亚洲欧美94在线 | 成人av免费 | 亚洲综合一区二区三区 | 99久久精品国产一区二区三区 | 国产精品亚洲成在人线 | 国产精品一区二区免费 | 国产美女精品视频免费观看 | 精品成人在线视频 | 欧美日韩在线精品 | 欧美日韩亚洲系列 | 亚洲第一中文字幕 | 日韩欧美中文字幕在线观看 | 99精品视频在线观看 | 国产一级毛片精品完整视频版 |