c# - 使用 Azure ActiveDir oAuth2.0 JWT token 的 Asp-Net-Core WebApi 身份验证 Microsoft Graph

标签 c# oauth-2.0 azure-active-directory msal.js

我正在通过调整此 .net 核心 asp-net-core 模板 (https://github.com/TrilonIO/aspnetcore-Vue-starter/blob/master/content/Vue2Spa.sln) 来模拟 SPA(单页应用程序)

我能够使用 MSAL for microsoft graph 对用户进行身份验证。

如何将我用于 Microsoft graph 的相同标记用于我的 API(相同的项目,不需要 CORS)。

我尝试的一切都失败了。

StartUp 应该是什么样子?我不需要任何 cookie 身份验证,我已准备好作为 JWT token 传递的 AccessToken。?

我想在我的 API Controller 中使用 [Authorize]。

SPA --> 获取 token (它基于我的 clientId 等),通过 AJAX 调用传递 token “Bearer eyJ0eXAiO....”到我的 api Controller 。

最佳答案

我能够使用我在 MSAL 库中创建的 ID token 来做到这一点。

Javascript:返回 myMSALObj.loginPopup(requestObj).then(function (loginResponse) { loginResponse.Token});

然后您可以获取该 token 并将其传递给 API。

在 StartUp.cs 中

       public void ConfigureServices(IServiceCollection services)
       {
       services.AddAuthentication(options =>
       {
       options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
       options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
       }).AddJwtBearer(options =>
       {
       options.Authority = "https://sts.windows.net/tenantId/v2.0";
       options.Audience = clientId;
       options.TokenValidationParameters.ValidateLifetime = true;
       });

       services.AddAuthorization();

       // Add framework services.
       services.AddMvc()
       .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
       }```

关于c# - 使用 Azure ActiveDir oAuth2.0 JWT token 的 Asp-Net-Core WebApi 身份验证 Microsoft Graph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56799487/

相关文章:

spring-boot - 为什么在为访问 token 交换授权代码时收到无效 token 响应? ( Spring 启动、oauth2、天蓝色)

azure-active-directory - Azure AD B2C 登录名

Azure AD 身份验证回复地址不匹配

azure - Azure 中所有 Azure 资源类型的列表?

C# WPF - 无法选中/取消选中 DataGridCheckBoxColumn 中的复选框

c# - 如何包含 xsd :documentation in the C# class generated with XSD. exe?

c# - C++ 函数到 C#

c# - 如何在 C# 中反序列化 JSON 字符串是正确的?

google-api - 谷歌 OAuth 2 : response_type error on token request

Django Oauth Toolkit 2-legged 和 3-legged