【问题标题】:How do you add Identity server 4.0 as an external provider?如何将 Identity server 4.0 添加为外部提供者?
【发布时间】:2021-03-06 14:19:05
【问题描述】:

似乎所有消息来源都在谈论将外部提供者添加到 Identity Server 4 中,而不是使用 Identity Server 4 作为外部提供者。

我的 startup.cs 有这行用于 Facebook 身份验证:

            services.AddAuthentication(options =>
            {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
               
            })
            .AddFacebook(facebookOptions =>
            {
                 facebookOptions.AppId = "<appId>";
                 facebookOptions.AppSecret = "<appSecret>";
                 facebookOptions.SaveTokens = true;

             })

我的登录页面上有一个按钮:

<a class="btn btn-primary"
   asp-action="ExternalLogin"
   asp-route-provider="Facebook"
   asp-route-returnUrl="">
    Facebook
</a>

这会导致:

[HttpPost]
[HttpGet]
[AllowAnonymous]
public IActionResult ExternalLogin(string provider, string returnUrl = null)
{
    // Request a redirect to the external login provider.
    var redirectUrl = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl });
    var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);

    return Challenge(properties, provider);
}

这一切都有效。 我还有一个 Identity Server 4.0 服务器设置,我想将它用作另一个外部提供程序。

            services
            .AddAuthentication(options =>
            {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;

            })
            .AddFacebook(facebookOptions =>
            {
                 facebookOptions.AppId = "<appId>";
                 facebookOptions.AppSecret = "<appSecret>";
                 facebookOptions.SaveTokens = true;

             })
            .AddIdentityServer(identityServerOptions=> //Doesn't exist? or Does it?
            {
                 identityServerOptions.AppId = "<appId>";
                 identityServerOptions.AppSecret = "<appSecret>";
                 identityServerOptions.SaveTokens = true;

             })

如何将身份服务器添加为外部身份验证提供程序?

**更新:** 来自This site

@if (Model.ExternalProviders.Any())
{
    <div class="row">
        <div class="panel-body">
            <ul class="list-inline">
                @foreach (var provider in Model.ExternalProviders)
                {
                    <li>
                        <a class="btn btn-default"
                           asp-action="ExternalLogin"
                           asp-route-provider="@provider.AuthenticationScheme"
                           asp-route-returnUrl="@Model.ReturnUrl">
                            @provider.DisplayName
                        </a>
                    </li>
                }
            </ul>
        </div>
    </div>
}

provider 好像和 Authentication Scheme 一样

【问题讨论】:

    标签: asp.net-core authentication identityserver4 openid-connect


    【解决方案1】:

    Identityserver 是经过认证的 OpenId Connect 提供程序,因此您可以只使用:

    .AddOpenIdConnect(authenticationScheme, displayName, options =>
    {
      options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
      options.Authority = "<base-path-of-your-external-IS>";
      options.ClientSecret = "<appSecret>";
      options.ClientId = "<appId>";
      options.ResponseType = OpenIdConnectResponseType.Code;
      options.SaveTokens = true;
    }
    

    【讨论】:

    • 太棒了!谢谢。你知道我将与 _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl); 一起使用的提供商名称吗?
    • 也许您根本不需要 SignInManager?如果您只需要创建一个身份验证 cookie(基于外部身份验证)并保存令牌,它就可以在没有 ASP.Net Core Identity and Entity Framework 的情况下工作。您只需执行质询(重定向到外部 IdP),然后处理回调(调用 HttpContext.SignInAsync())。差不多就是这样。
    • 如果您仍然需要涉及 ASP.Net Core Identity,您是对的,请使用您喜欢的 authenticationScheme 名称
    猜你喜欢
    • 2021-03-14
    • 2020-06-21
    • 1970-01-01
    • 2014-04-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多