问题标签 [microsoft-identity-web]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
463 浏览

asp.net-core - 如何使用 Angular 12 和 ASP.Net Core 5 通过 Microsoft Single Sign-On 授权 Hangfire 仪表板

我的应用程序是在 ASP.Net Core 5 上运行的 Angular 12 应用程序。

我目前正在尝试锁定 Hangfire,以便它仅适用于具有管理员角色的人。

它使用 Microsoft 身份登录 - 特别是在 Azure 中设置的单点登录。

这适用于我的点网核心控制器。

我需要做的就是添加 Authorize 属性:

但是当我想在 Hangfire 中进行授权时,用户对象缺少很多属性。

这是 HangfireAuthorisationFilter:

但是,有 cookie 信息,其中包含 msal cookie: httpContext.Request.Cookies

如何将身份验证信息传递给 Hangfire Authorize 方法?如何访问角色信息,以便将其锁定为仅管理员角色?有没有办法可以在服务器端解码 msal cookie?

0 投票
2 回答
129 浏览

asp.net-core - Microsoft.Identity.Web.UI 在本地工作,但不适用于应用服务

我一直在尝试将身份验证添加到我的网络应用程序中:https ://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/1-WebApp-OIDC/1- 1-我的组织

如果我通过 appsettings.Development.json 在本地运行dotnet run,我可以按预期使用我的组织凭据登录。当我将容器化并部署到 Azure 中的 Web 应用程序时,我没有成功登录。浏览器中的 url 保持在 /signin-oidc 并从默认的 Razor 页面应用程序转到错误页面。

应用服务日志有消息说.AspNetCore.Correlation.OpenIdConnect.[key?] cookie not found

[更新] 身份验证流程在我的手机上有效,但在桌面上无效。

  1. 为什么相同的代码可以在本地工作但不能部署?
  2. 为什么找不到cookie?
  3. 为什么它适用于 iOS 而不是 Windows?
0 投票
0 回答
299 浏览

c# - 使用 Microsoft.Identity.Web 1.60 调用 Microsoft Graph 时出现 System.NullReferenceException

我正在将应用程序从 .NET Core 2.2 升级到 .NET 5.0。在此过程中,我需要更新 Microsoft.Graph 和 Microsoft.Identity.Web。我更新为使用新配置,但是当我尝试获取用户详细信息时,Graph 抛出 System.NullReferenceException。

这是我的服务配置

我的 appsettings.json 确实具有所需的值

这是调用图形 API 的代码

该代码适用于我使用旧界面配置 Graph 并使用旧版本 Microsoft.Identity.Web 获取静默令牌的当前应用程序。不知道为什么新版本似乎无法获得 Graph 令牌。应用程序的身份验证通常有效,并且我确实有一个经过身份验证的用户,它仅在调用 MS Graph 时失败。

这是错误的参考日志条目

谢谢

0 投票
1 回答
361 浏览

azure - Setting Different ReturnUrl in State Parameter with Microsoft.Identity.Web

I have an ASP.NET Core web app that is authenticating with Azure AD in a multi-tenant configuration using Microsoft.Identity.Web. We use a tenant/company identifier as the subdomain of our apps URL. (companyA.myapp.com, companyB.myapp.com). Some users have access to more than one tenant of the application, so we cannot map a Azure AD tenant directly to a single tenant/company in our app.

With Microsoft.Identity.Web, how is the state parameter set or manipulated as described here? I would like to follow the guidance provided here, but am not sure where to start. https://docs.microsoft.com/en-us/azure/active-directory/develop/reply-url#use-a-state-parameter

If you have several subdomains and your scenario requires that, upon successful authentication, you redirect users to the same page from which they started, using a state parameter might be helpful.

In this approach:

  1. Create a "shared" redirect URI per application to process the security tokens you receive from the authorization endpoint.
  2. Your application can send application-specific parameters (such as subdomain URL where the user originated or anything like branding information) in the state parameter. When using a state parameter, guard against CSRF protection as specified in section 10.12 of RFC 6749).
  3. The application-specific parameters will include all the information needed for the application to render the correct experience for the user, that is, construct the appropriate application state. The Azure AD authorization endpoint strips HTML from the state parameter so make sure you are not passing HTML content in this parameter.
  4. When Azure AD sends a response to the "shared" redirect URI, it will send the state parameter back to the application.
  5. The application can then use the value in the state parameter to determine which URL to further send the user to. Make sure you validate for CSRF protection.
0 投票
1 回答
291 浏览

asp.net-core - Azure 广告身份验证中的问题。错误:无法取消保护消息。状态

当我尝试进行身份验证时,出现以下错误

我创建了一个核心 mvc 站点,它在 stage 和 dev 中运行良好,但 prod 失败了。我们正在使用 linux docker 来托管 azure app 服务。我们调查并发现当 azure 尝试在 prod 中运行多个实例时会发生此问题。我们在下面找到了一个关于数据保护的链接,但我需要详细了解我需要做什么。 Kubernetes 中的 Azure AD 身份验证无法取消对 message.State 的保护

在 startup.cs 中,身份验证按以下方式配置

0 投票
0 回答
179 浏览

azure - Azure 函数中的 Microsoft.Identity.Web

我需要从我的 Azure 函数内部调用下游 API,即 PowerBI 服务。我在 Web 应用程序中有一个工作示例,但我无法在我的 Azure 函数中实现它。

在 Web 应用示例中,配置通过 Microsoft.Identity.Web 进行,如下所示。

然后在控制器内部,我能够接收到构造函数中注入的 ITokenAcquisition 对象

我可以这样做:

并获得这样的令牌凭证:

使用这个 tokenCredentials 我可以实现我的目标,即实例化一个 PowerBIClient 类以与 PowerBI 服务进行交互。

问题是我无法在隔离的 Azure Function 的 Startup 类中设置配置。因此,我无法在我的 Azure 函数中注入 ITokenAcquisition 以获取访问令牌。

有人可以请教吗?太感谢了。

0 投票
1 回答
51 浏览

c# - Why does the official Microsoft tutorial have me push two copies of the same dotnet core app as the "frontend" and "backend"?

I'm pretty new to Azure, and I wanted to get familiar with using Azure AD auth as an authentication provider for a dotnet core web app.

I followed this tutorial: https://docs.microsoft.com/en-us/azure/app-service/tutorial-auth-aad?pivots=platform-linux

It worked as described, but I have to be missing something, because the way it's deployed doesn't make any sense to me.

The "starting point" tutorial code is this repo, here: https://github.com/Azure-Samples/dotnet-core-api

It's a simple dotnet core web app that hosts an Angular frontend. Through the course of the tutorial, you make some changes to get the "frontend" to talk to the "backend", and you add two remotes to your Git repo - frontend and backend - but they're on the same repo.

As I understand it, at the end of this tutorial, I have two nearly-identical Azure App Service instances running in my Azure resource group. They are both running the same code. Both include the full-stack application - both are hosting a Kestrel or HTTP.sys (or whatever) instance on port 443 at their respective .azurewebsites.net URL - both show my index.html page - but one is supposed to be the "frontend" and the other is supposed to be the "backend". The only thing different about the "frontend" and "backend" is that the "backend" has an identity provider configured, and the "frontend" has API permissions granted to it from the "frontend".

Is it really necessary to have the whole service running in both places? Is there a way to host the actual dotnet core API service as an App Service, host the HTML / JS / CSS separately as an Azure Static Page (or something similar), and still configure the identity provider? Or does it have to be like this? It seems like overkill, from a technical standpoint, as well as from a billing standpoint.

0 投票
1 回答
125 浏览

c# - 如何使用 .net5 从 MVC C# 控制器向现有 Microsoft 身份用户添加新角色

我有一个应用程序,用户使用 Azure AD 登录,然后将详细信息存储在数据库中。现在,如果用户存在于数据库中,他们将被分配一个角色。这个角色被添加到 startup.cs 中的声明中,这一切都很好。但是,当用户未存储在数据库中并且需要创建时。那么问题是我如何设法使用 UserController.cs 中的用户角色更新 cookie

这是我的 startup.cs ConfigurationService()

对于存储在数据库中的用户,这是将角色添加到声明的位置

应用程序端点是

对于当前未存储在数据库中的用户,他们会在创建视图中显示,他们需要在保存到数据库之前完成一些附加信息。风景

获取新用户

新用户完成附加信息后。在这里,我想使用角色声明更新身份和 cookie,而无需用户注销并重新登录。

这是我的视图的用户模型

还将有一个用户编辑页面,用户可以在其中更改更高级别的用户的角色。有四个主要角色“超级用户、管理、管理员、员工”所有新用户都将被赋予员工角色,直到它被更高级别的人更改。

我试图在谷歌上寻找一些关于这种情况的可靠例子,并且已经指出了 UserManager 和 RoleManager 的方向,但由于之前缺乏使用这些知识的知识,我一直无法让它发挥作用。我已经看到一些使用 IClaimTransformation 但我再次无法使用我找到的唯一示例来使用它。如果有人可以帮助指出需要的步骤或指出我可以使用参考点的好例子,我将不胜感激。

0 投票
1 回答
73 浏览

asp.net-core - 使用 Microsoft.Identity.Web 时,在 401 Unauthorized 响应中包含自定义 WWW-Authenticate 标头

按照有关使 MS Office 连接到我的 Asp.NET Core Web API 的说明,我正在尝试向 MS Office 提供登录重定向以失败的身份验证。以下问题答案我试图在WWW-Authenticate标题属性中包含登录重定向信息。我的 Web API 受 Azure AD 和Microsoft.Identity.Web库的保护。当身份验证失败并且中间件返回 401 Unauthorized 响应时,标头确实包含该WWW-Authenticate属性,但其值为 only Bearer

问:如何更新标头信息以包含必要的附加重定向信息?

我试图在 API 上实现一个属性,该属性派生自IAsyncAuthorizationFilter并访问其中的响应标头。但是,在调用此属性之前,中间件已经返回 401。

0 投票
2 回答
320 浏览

asp.net-core - GraphServiceClient 无法在 Razor 页面模型中使用 Microsoft Identity Web

我有一种情况,我可以在我的 Startup 中成功使用 GraphServiceClient,但不能在我的 Razor 页面中使用。我正在将 Microsoft Identity Web 与 Microsoft Graph 一起使用,并在此处遵循了 MS 文档,还检查了许多其他类似的问题,但我总是以同样的错误告终......

MsalUiRequiredException:没有帐户或登录提示传递给 AcquireTokenSilent 调用

请注意,我的应用注册在我的租户中,但用户帐户在客户租户中。因此,我的应用身份验证配置为“任何组织目录中的帐户(任何 Azure AD 目录 - 多租户)”和以下 API 权限...

在此处输入图像描述

我的设置如下,虽然它是一个多租户应用程序,但我只有一个客户,因此将 TenantId 设置为我的客户租户 ID,以获得简化的登录体验。

我的 Startup 具有以下 ConfigureServices 方法。请注意,对 graphServiceClient.Me.Request().GetAsync() 的调用在这里有效,并从我的客户 Azure AD 返回用户信息。当用户登录到我的应用程序时,我计划使用它来用额外的用户信息(即照片)填充我的本地数据库。

这是我的 Razor 页面,但它总是在同一个图形调用上失败,即使它在我的启动文件中工作。我已经尝试将范围从“https://graph.microsoft.com/v1.0”更改为“user.read”,但在这一行仍然失败。

var user = await _graphServiceClient.Me.Request().GetAsync();

这是错误的堆栈跟踪。