我正在使用Azure B2C成功地在Blazor应用程序中登录和退出,但我不清楚如何正确地定义SignedOut页面。这个问题似乎更适用于Microsoft.Identity.Web.UI,,因为这个SignedOut页面似乎是硬编码到一个非常通用的SignedOut.cshtml上:
签出 你已经成功地签了名。
文档似乎表明这是可以更改的,但它没有说明具体如何更改。
在文档中:“默认情况下,注销URL显示已签名的视图页SignedOut.cshtml.cs。此页面也作为MIcrosoft.Identity.Web的一部分提供。”
考虑到我正在编写Blazor应用程序,我尝试创建一个SignedOut.razor组件,但这并没有覆盖它:
@page "/MicrosoftIdentity/Account/SignedOut"这是Microsoft.Identity.Web.UI的来源。你可以看到它是硬编码的。
public IActionResult SignOut([FromRoute] string scheme)
{
if (AppServicesAuthenticationInformation.IsAppServicesAadAuthenticationEnabled)
{
return LocalRedirect(AppServicesAuthenticationInformation.LogoutUrl);
}
else
{
scheme ??= OpenIdConnectDefaults.AuthenticationScheme;
var callbackUrl = Url.Page("/Account/SignedOut", pageHandler: null, values: null, protocol: Request.Scheme);
return SignOut(
new AuthenticationProperties
{
RedirectUri = callbackUrl,
},
CookieAuthenticationDefaults.AuthenticationScheme,
scheme);
}
}发布于 2020-12-03 03:17:28
Microsoft.Identity.Web v1.9
更新:这是我最喜欢的方法
只需将其添加到配置下的startup.cs中即可。在这里,我刚刚重定向到我的主页,但您可以重定向到您自己的自定义登录页,如果您愿意。
app.UseRewriter(
new RewriteOptions().Add(
context =>
{
if (context.HttpContext.Request.Path == "/MicrosoftIdentity/Account/SignedOut")
{
context.HttpContext.Response.Redirect("/");
}
}));方法#2
在写这个问题的时候,我找到了一种很简单的方法。这似乎仍然很奇怪,这是预期的方式,所以请随时改进或添加更好的答案。我怀疑新版本会让这件事变得更容易。
因为Microsoft.Identity.Web.UI是一个可重用类库(RCL),所以只要将它添加到同一个位置的web应用程序中,任何页面都可以被覆盖。
正如您所看到的,我几乎通过创建自己的SignedOut.razor页面并给它与URL相同的路径来完成这一任务。这不起作用,因为它是一个剃须刀组件,它必须匹配源中的路径,而不是web应用程序中的URL。
谢天谢地,它是开源的。我必须在这里找到这条路,因为这对我来说并不明显。https://github.com/AzureAD/microsoft-identity-web
因此,这里是您在项目中需要的正确路径,也是我所能找到的最佳答案,即为您自己提供一个真正的SignedOut页面。如果您不想要一个SignedOut页面,我想您必须在这里添加一个重定向。
Areas/MicrosoftIdentity/Pages/Account/SignedOut.cshtml
发布于 2021-12-04 07:10:00
我遵循上面提供的Jason的解决方案,并将SignedOut.cshtml文件添加到我的Blazor项目中。然后,我修改了OnGet()方法,以重定向我的blazor应用程序的主页:
public class SignedOut : PageModel
{
public ActionResult OnGet()
{
return Redirect("/");
}
}作为参考,我的项目文件夹布局看起来有点像这样:
- Pages
- Shared
- wwwroothttps://stackoverflow.com/questions/65119397
复制相似问题