简体   繁体   English

如何从 Blazor 组件调用“/Identity/Account/ExternalLogin”?

[英]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.

相关问题 如何从另一个 Blazor(“子”)组件调用一个 Blazor(父)组件中的方法? - How do I call a method in one Blazor (parent) component from another Blazor (“child”) component? 如何从 Blazor 服务器中的页面处理(销毁)组件? - How can I dispose (destroy) a component from a page in Blazor Server? 如何创建 blazor 组件? - How can i create a blazor component? 如何将列表从子组件传递到 blazor 中的父组件? - How can I pass List from child component to parent component in blazor? 如何从 blazor 中的子项调用父 razor 组件中的 function? - How to call a function in a parent razor component from a child item in blazor? 如何正确使用依赖注入在 Blazor 组件上包含 asp.net 核心标识 UserManager? - How do I properly use dependency injection to include asp.net core identity UserManager on a Blazor component? 我如何像Vue那样从父层随机添加CSS属性到Blazor组件? - How can I randomly add CSS attributes to Blazor component from parent layer as Vue did? 如何在 Blazor 中呈现定义为对象的组件? - How can I render a component in Blazor that is defined as object? 如何在 Blazor(子)组件中引用“@typeparam TModel”字段? - How can I reference a "@typeparam TModel" field in the Blazor (sub)-component? 如何为 blazor 集成 Radzen 列表框组件? - How can i integrate Radzen listbox component for blazor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM