[英]How can I call '/Identity/Account/ExternalLogin' from a Blazor component?
My goal is to have the external provider buttons (exactly like the foreach from the /Identity/Account Login and Register pages, see below) in a Blazor component.我的目标是在 Blazor 组件中拥有外部提供程序按钮(与 /Identity/Account 登录和注册页面中的 foreach 完全相同,见下文)。 I am using the "Blazor Server App" template.
我正在使用“Blazor 服务器应用程序”模板。
I have successfully pulled in the external providers, enumerated buttons, and have a form with the same action as the default identity login page.我已成功引入外部提供程序、枚举按钮,并拥有一个与默认身份登录页面具有相同操作的表单。 My external login is with Twitter and is confirmed working on the default page.
我的外部登录是 Twitter 并确认在默认页面上工作。
<form action="/Identity/Account/ExternalLogin" method="post">
@foreach (var provider in ExternalLogins)
{
<button type="submit" class="btn btn-primary btn-lg form-control" name="provider"
value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button>
}
</form>
When I use the button to login from the blazor component, the debug window shows a Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException
being thrown.当我使用按钮从 blazor 组件登录时,调试 window 显示
Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException
被抛出。 My only guess is that this is because the navigation is not initiating from one of the built-in identity pages that probably have Antiforgery elements.我唯一的猜测是,这是因为导航不是从可能具有 Antiforgery 元素的内置身份页面之一启动的。 How do I directly navigate to this ExternalLogin page without the middle-man login/register page?
如何在没有中间人登录/注册页面的情况下直接导航到此 ExternalLogin 页面?
Calling the /Identity/Account/ExternalLogin page means taking care of anti-CSRF mechanism (HTTP header and form's hidden field) and it's not easy to implement.调用/Identity/Account/ExternalLogin页面意味着要处理反 CSRF 机制(HTTP header 和表单的隐藏字段),实现起来并不容易。
Instead of directly call /Identity/Account/ExternalLogin it's easier to customize the login page:与其直接调用/Identity/Account/ExternalLogin ,不如自定义登录页面更容易:
Starting from the default blazor template with authentication (wasm or server), scaffold Identity items, depending of what you need, but at least the login page and external login pages to customize them.从默认的 blazor 模板开始,带有身份验证(wasm 或服务器),脚手架标识项,取决于您的需要,但至少有登录页面和外部登录页面来自定义它们。
Select the Areas folder and right click to open the contextual menu. Select Areas 文件夹并右键单击以打开上下文菜单。 The choose Add -> New Scaffolded Item...
选择 Add -> New Scaffolded Item...
Choose Identity and click Add.选择身份并单击添加。
Select your Identity data context class and what you want to scaffold. Select 您的身份数据上下文 class 以及您想要搭建的内容。
Update Areas/Identity/Pages/Account/Login.cshtml for your needs.根据您的需要更新Areas/Identity/Pages/Account/Login.cshtml 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.