如何在主页而不是登录页面中设置外部登录

问题描述 投票:0回答:1

在我的应用程序中,我需要导航栏中的外部google登录按钮。我现在有一个登录按钮,它将带您到外部登录所在的登录屏幕。但对于我的项目,我将不使用常规登录名。有帮助吗?

我试图在主页上设置按钮,但它说:未找到提供者。

c# authentication asp.net-core google-authentication blazor
1个回答
0
投票

您可以设置登录页面以便更改它们:

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-3.0&tabs=visual-studio#create-full-identity-ui-source

或者您也可以在.razor页面上更改登录按钮:

  <a class="ml-md-auto btn btn-primary"
       href="/Login"
       target="_top">Login</a>

进入将立即将其引导至Google进行登录的页面:

namespace BlazorGmail.Server.Pages
{
    [AllowAnonymous]
    public class LoginModel : PageModel
    {
        public IActionResult OnGetAsync(string returnUrl = null)
        {
            string provider = "Google";
            // Request a redirect to the external login provider.
            var authenticationProperties = new AuthenticationProperties
            {
                RedirectUri = Url.Page("./Login", 
                pageHandler: "Callback", 
                values: new { returnUrl }),
            };
            return new ChallengeResult(provider, authenticationProperties);
        }
        public async Task<IActionResult> OnGetCallbackAsync(
            string returnUrl = null, string remoteError = null)
        {
            // Get the information about the user from the external login provider
            var GoogleUser = this.User.Identities.FirstOrDefault();
            if (GoogleUser.IsAuthenticated)
            {
                var authProperties = new AuthenticationProperties
                {
                    IsPersistent = true,
                    RedirectUri = this.Request.Host.Value
                };
                await HttpContext.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                new ClaimsPrincipal(GoogleUser),
                authProperties);
            }
            return LocalRedirect("/");
        }
    }
}

请参阅:Google Authentication in Server Side Blazor

© www.soinside.com 2019 - 2024. All rights reserved.