BlazorMSAL没有请求范围
创始人
2024-12-21 12:00:28
0

Blazor中的MSAL(Microsoft身份验证库)可能无法正确请求所需的访问令牌范围。要解决此问题,您可以手动在代码中指定所需的访问令牌范围。

下面是一个代码示例,展示如何在Blazor中使用MSAL并请求特定的访问令牌范围:

@page "/secure"
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Microsoft.Graph
@inject GraphServiceClient GraphClient
@inject RefreshTokenService TokenService




@if (initializing)
{
    

Loading...

} else if (context == null) {

You are not signed in.

} else {

Welcome, @context.User.Identity.Name! You have access to the following scopes:

    @foreach (var scope in context.Scopes) {
  • @scope
  • }
} @code { private bool initializing = true; private RemoteAuthenticationState authState; private AuthenticationState authStateFromModal; private IAccessTokenProvider TokenProvider; private GraphServiceClient graphClient; private AuthenticationResult context = null; protected override async Task OnInitializedAsync() { TokenProvider = (IAccessTokenProvider)TokenService; graphClient = GraphClient; authState = await TokenProvider.RequestAccessToken(); context = await TokenProvider.RequestAccessToken(new AccessTokenRequestOptions { Scopes = new[] { "user.read", "mail.send" } }); initializing = false; } private async Task SignIn() { authStateFromModal = await TokenProvider.RequestAccessToken(new AccessTokenRequestOptions { Scopes = new[] { "user.read", "mail.send" } }, authState); context = authStateFromModal.GetAuthenticationState(); StateHasChanged(); } private async Task SignOut() { await TokenProvider.ClearCache(); context = null; StateHasChanged(); } }

在上面的代码中,我们在OnInitializedAsync方法中使用MSAL的RequestAccessToken方法请求所需的访问令牌范围。在这种情况下,我们请求user.readmail.send访问令牌范围。

创建AuthConfig.cs文件 并添加以下代码:

public const string ClientId = "";
public const string Authority = "https://login.microsoftonline.com/";
public const string RedirectUri = "";
public const string GraphUri = "https://graph.microsoft.com/v1.0";

请将 "your-application-client-id","your-directory-tenant-id" 和 "your-application-redirect-uri" 替换为您的应用程序的相关信息。

在您的服务中注册以下内容:

builder.Services.AddMsalAuthentication(options =>
{
    options.ProviderOptions.Cache.CacheLocation = "localStorage";
    options.ProviderOptions.DefaultAccessTokenScopes.Add("user.read");
    options.ProviderOptions.DefaultAccessTokenScopes.Add("mail.read");
    options.ProviderOptions.DefaultAccessTokenScopes.Add("openid");
});

然后您需要在Startup.cs中启用并配置SPA服务,以使应用程序能够使用身份验证。

app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app

相关内容

热门资讯

黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai,(红龙扑克下载)一直存在有挂,爆料教程(发现有挂);亲,有的,ai...
黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai辅助工具,(扑克之城)好像是真的有挂,攻略教程(有挂方法)是一款可以...
黑科技辅助!wepoke辅助插... 黑科技辅助!wepoke辅助插件安装,(菠萝德州软件)原先是真的有挂,黑科技教程(详细教程);菠萝德...
黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai辅助工具,(德州ai盈利)一直是有挂,AI教程(有挂讲解);德州ai...
黑科技辅助!wepoke透明黑... 黑科技辅助!wepoke透明黑科技,(AAPoKer模拟器)往昔是有挂,科技教程(真的有挂);AAP...
黑科技辅助!wepoke辅助插... 黑科技辅助!wepoke辅助插件安装,(微扑克机器人)总是存在有挂,揭秘教程(果真有挂);微扑克机器...
黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai辅助工具,(德扑之星代打)都是真的有挂,必胜教程(今日头条),德扑之...
黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai辅助工具,(AAPoKerai机器人)真是是有挂,力荐教程(有挂辅助...
黑科技辅助!wepoke有挂真... 黑科技辅助!wepoke有挂真的有挂,(红龙扑克机制)原来有挂,高科技教程(有挂教程);亲,有的,a...
黑科技辅助!wepoke辅助插... 黑科技辅助!wepoke辅助插件,(WePoKe黑科技)先前真的是有挂,2025新版教程(发现有挂)...