在我的应用程序中,我需要导航栏中的外部google登录按钮。我现在有一个登录按钮,它将带您到外部登录所在的登录屏幕。但对于我的项目,我将不使用常规登录名。有帮助吗?
我试图在主页上设置按钮,但它说:未找到提供者。
您可以设置登录页面以便更改它们:
或者您也可以在.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("/");
}
}
}