错误:由于对用户的挑战而抛出了msaluirequiredException
我正在尝试进行基本的Microsoft Graph API调用,但是每当我调用getAccessTokenForuserAsync()
函数以获取令牌时,我会收到此错误。这是ASP.NET Core 6 MVC应用程序。
program.cs:
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
// Add services to the container.
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
.EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
.AddMicrosoftGraph(builder.Configuration.GetSection("MicrosoftGraph"))
.AddInMemoryTokenCaches();
appsettings.json:
"MicrosoftGraph": {
"BaseUrl": "https://graph.microsoft.com/v1.0",
"Scopes": "user.read"
},
控制器:
[AuthorizeForScopes(Scopes = new string[] { "user.read" })]
public class UserManagementController : Controller
{
...
[AuthorizeForScopes(Scopes = new string[] { "user.read" })]
public async Task<IActionResult> Index()
{
try
{
var accessToken = await _tokenAcquisition.GetAccessTokenForUserAsync(new string[] { "user.read" });
return View();
}
catch(Exception ex)
{
return BadRequest("An error occurred loading the user management page.");
}
}
}
我尝试过许多解决方案。错误总是相同的:
由于用户面临挑战,因此抛出了msaluirequiredException。没有帐户或登录提示传递给AccelireTokenSilent Call
我只是想让基本知识正常工作,没有任何幻想。
I am attempting to make a basic Microsoft Graph API call but I get this error whenever I call the GetAccessTokenForUserAsync()
function to get a token. This is an ASP.NET Core 6 MVC application.
Program.cs:
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
// Add services to the container.
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
.EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
.AddMicrosoftGraph(builder.Configuration.GetSection("MicrosoftGraph"))
.AddInMemoryTokenCaches();
appSettings.json:
"MicrosoftGraph": {
"BaseUrl": "https://graph.microsoft.com/v1.0",
"Scopes": "user.read"
},
Controller:
[AuthorizeForScopes(Scopes = new string[] { "user.read" })]
public class UserManagementController : Controller
{
...
[AuthorizeForScopes(Scopes = new string[] { "user.read" })]
public async Task<IActionResult> Index()
{
try
{
var accessToken = await _tokenAcquisition.GetAccessTokenForUserAsync(new string[] { "user.read" });
return View();
}
catch(Exception ex)
{
return BadRequest("An error occurred loading the user management page.");
}
}
}
I've tried many solutions to this. The error is always the same:
An MsalUiRequiredException was thrown due to a challenge for the user. No account or login hint was passed to the AcquireTokenSilent call
I'm just trying to get the basics working, nothing fancy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请让我在这里分享一个例子。
首先,让我们添加MS Identity UI,创建一个名为
_loginPartial.cshtml
inviews/sharone
文件夹中的部分视图。然后将部分视图添加到
_Layout.cshtml
:&lt; partial name =“ _ loginPartial”/&gt;
然后在
program.cs.cs
中,通过设置Azure Ad agentication以下代码,并在app.useeauthorization();
之前添加app.useateentication();
:这些需要以下包装:
然后
appSettings.json
,添加类似下面的属性:然后添加
[授权]
为HOME Controller添加,以便您在渲染索引页面之前要求您登录。Pls allow me share an example here.
At first, let's adding ms identity ui, create a partial view named
_LoginPartial.cshtml
inViews/Shared
folder.Then adding the partial view into
_Layout.cshtml
:<partial name="_LoginPartial" />
Then in the
Program.cs
, set the azure ad authentication by following code, and addingapp.UseAuthentication();
beforeapp.UseAuthorization();
:These require following packages:
Then in
appsettings.json
, adding properties like below:Then adding
[Authorize]
for the home controller so that it will ask you to sign in before rendering index page.