如何解决外部登录不起作用Identity Server 4 ASP.NET Core
外部服务配置不适用于身份服务器4。
public static void ConfigureExternalOidcProvider(this IServiceCollection services)
{
services.AddAuthentication().AddGoogle("Google",options =>
{
options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
options.ClientId = "xxxxxxxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com";
options.ClientSecret = "xxxxxxxxxxxxxxx";
}).AddFacebook(options =>
{
options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
options.AppId = "xxxxxxxxxxxxx";
options.AppSecret = "xxxxxxxxxxxxxxxxxxx";
});
}
在外部控制器中,该模式为null,但是即使它不起作用,我也用IdentityServerConstants.ExternalCookieAuthenticationScheme
对该模式进行了硬编码。
public IActionResult Challenge(string scheme,string returnUrl)
{
if (string.IsNullOrEmpty(returnUrl)) returnUrl = "~/";
// validate returnUrl - either it is a valid OIDC URL or back to a local page
if (Url.IsLocalUrl(returnUrl) == false && _interaction.IsValidReturnUrl(returnUrl) == false)
{
// user might have clicked on a malicious link - should be logged
throw new Exception("invalid return URL");
}
// start challenge and roundtrip the return URL and scheme
var props = new AuthenticationProperties
{
RedirectUri = Url.Action(nameof(Callback)),Items =
{
{ "returnUrl",returnUrl },{ "scheme",scheme },}
};
return Challenge(props,scheme);
}
当我单击Google和Facebook按钮时,永远不会重定向到Google或Facebook页面。
这是我的调试选项的结果
Google点击
Facebook点击
CSHTML代码
@if (Model.VisibleExternalProviders.Any())
{
@foreach (var provider in Model.VisibleExternalProviders)
{
<a class="@($"mdc-button mdc-button--outlined ml-1 mb-1 {(provider.AuthenticationScheme == "Google" ? "mdl-button--googleplus google-logo" : "mdl-button--facebook")}")"
asp-controller="External"
asp-action="Challenge"
asp-route-provider="@provider.AuthenticationScheme"
asp-route-returnUrl="@Model.ReturnUrl">
@if (provider.AuthenticationScheme == "Google")
{
<i class="fab fa-google fa-fw google-logo"></i>
}
else
{
<i class="fab fa-facebook-square fa-fw"></i>
}
@provider.DisplayName
</a>
}
}
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。