我有一个 Net6 Blazor WASM 项目,其中该项目的所有页面/路由都需要通过 Azure Entra 身份平台进行“授权”。我想分享需要授权的页面的链接。但是,通过 Azure Entra 授权后,作为应用程序注册的一部分,用户将被重定向到
https://my-website.azurewebsites.net/.auth/login/aad/callback
。
用户案例: 我想发送一个需要身份验证的网页链接,即
https://my-website.azurewebsites.net/custom-link
。用户登录 Entra,登录后将被重定向到网站链接。
我如何将用户重定向回“自定义链接”网址而不是主页?
我们将在 blazor wsam 应用程序 Program.cs 中包含以下代码
builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
});
将鼠标放在VS中的
Authentication
上并按F12,我们会看到如下截图的属性,
它定义了默认的重定向URL,以便我们可以在appsettings.json中修改它,我有如下配置,就像你看到的我尝试将重定向URL转换为
/counter
。
"AzureAd": {
"Authority": "https://login.microsoftonline.com/tenant_id",
"ClientId": "client_id",
"ValidateAuthority": true,
"RedirectUri": "/counter"
}
然后我还需要将其添加到Azure AD中,然后我将
@attribute [Authorize]
放在FetchData刀片中,这样我访问索引页时就不需要登录了。
我这边进行了测试,当我点击访问FetchData刀片时,它要求我登录,但是使用默认的弹出模式会导致这个问题。
因此,我将模式更改为
redirect
builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
options.ProviderOptions.LoginMode = "redirect";
});
然后它在我这边起作用了,当我访问 FetchData 时,它会重定向到登录页面。登录成功后,会重定向到 Counterblade。