gpt4 book ai didi

.net - Microsoft Graph 身份验证重新处理其他声明

转载 作者:行者123 更新时间:2023-12-03 02:12:31 24 4
gpt4 key购买 nike

我正在构建一个简单的演示应用程序 netcoreapp6.0,其中提供了一个登录按钮来让用户登录 Azure AD。对于初始登录,我们请求基本声明,例如“user.read”。现在,我们需要在网站上打开特定页面后立即从用户那里获取更多声明。

在用户经过身份验证后,我无法解决如何获取这些声明。Microsoft 在 https://developer.microsoft.com/en-us/graph/graph-explorer 上有同意按钮,这正是我所需要的。但是,我找不到有关他们正在使用的 /common/reprocess/ 端点的任何文档。

到目前为止我们已经尝试过:

添加 claim 政策:

services.AddAuthorization(options => {
options.AddPolicy("ClaimsTest", policy => policy.RequireClaim("Contacts.Read"));
options.AddPolicy("MustHaveOneDrive", policy => policy.RequireClaim("Files.ReadWrite"));
});

然后检查这些声明,例如:

[Authorize(Policy="MustHaveOneDrive")]

这有效。如果用户提供声明,访问将被拒绝。我现在想让应用程序向用户询问所需的声明,就像 Microsoft 在 i graph explorer 中所做的那样。

我无法为此提供代码,因为我们不知道从哪里开始。

最佳答案

感谢迄今为止的帮助!

我们已经成功了:

我们做了什么:在startup.cs

services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("Authentication")) // Fetch Auth Data from appsettings.json
.EnableTokenAcquisitionToCallDownstreamApi(initialScopes) // Middleware to acquire additional scopes
.AddMicrosoftGraph(Configuration.GetSection("DownstreamApi")) // Microsoft graph to fetch user information, files, whatever
.AddInMemoryTokenCaches(); // Should be done externally!

基本上就是这样。不太难,但很难找到(对我们来说)。

设置中间件后,我们无法执行此操作(工作示例):

somePage.cshtml.cs:

为您可能想要增量添加的任何即将推出的范围添加注释标记。

namespace Development_Praxisworkshop.Pages;

[AuthorizeForScopes(Scopes = new[]{"files.readwrite", "Sites.Read.All"})] // What scopes are we going to request eventually
public class OneDriveFilesModel : PageModel
{
public IDriveItemsCollectionPage files;
public IDriveItemChildrenCollectionPage _files;

private readonly ILogger<PrivacyModel> _logger;
private readonly IConfiguration _config;
private readonly ITokenAcquisition _tokenAcquisition;
private string _accessToken;
private readonly GraphServiceClient _graphServiceClient;
private readonly MicrosoftIdentityConsentAndConditionalAccessHandler _consentHandler;

public OneDriveFilesModel(ILogger<PrivacyModel> logger,
IConfiguration config,
ITokenAcquisition tokenAcquisition,
GraphServiceClient graphServiceClient,
MicrosoftIdentityConsentAndConditionalAccessHandler consentHandler)
{
_logger = logger;
_config = config;
_tokenAcquisition = tokenAcquisition;
_graphServiceClient = graphServiceClient;
this._consentHandler = consentHandler;
}

public void OnGet()
{
string[] scopes = new string[]{"files.readwrite", "Sites.Read.All"}; // scopes to be incrementally requested
_accessToken = _tokenAcquisition.GetAccessTokenForUserAsync(scopes).Result;
Console.WriteLine(_accessToken); // check token!
_files = _graphServiceClient.Me.Drive.Root.Children.Request().GetAsync().Result;
}
}

如果用户尚未提供一组新的声明,现在将请求一组新的声明。希望这对将来的人有所帮助!

如果我们在这里做了任何奇怪的事情,请随时提出建议!

appsettings.json 中的身份验证配置如下所示:

 "Authentication": {
"Instance": "https://login.microsoftonline.com/",
"ClientId":"00000000-0000-0000-0000-000000000000",
"ClientSecret": "we now need a client secret for the client to incrementally update the app registration in azure ad",
"TenantId":"contoso.com",
//"TenantId": "00000000-0000-0000-0000-000000000000"
//"TenantId": "common" //multi tenant apps
"ClientCapabilities": [ "cp1" ],
"CallbackPath": "/signin-oidc"
}

关于.net - Microsoft Graph 身份验证重新处理其他声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72909541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com