【问题标题】:How to convert ASPNetIdentity to OpenIdConnect如何将 ASPNetIdentity 转换为 OpenIdConnect
【发布时间】:2019-06-11 21:32:35
【问题描述】:

我有一个使用 .NET Framework 4.6 AspNetIdentity 的项目,我正在尝试升级它以使用 OpenIdConnect。有没有人使用 .NET Framework 4.6 成功地将 ASPNetIdentity 替换为 OpenIdConnect?

我查看了 owin 示例和一些 .NET core 2.0 快速入门示例,例如 these,但它们似乎与我想要完成的工作不兼容。

我正在尝试专门添加类似于以下代码 sn-p 的内容,该代码取自上述示例之一:

services.AddAuthentication(options =>
{
    options.DefaultScheme = "Cookies";
    options.DefaultChallengeScheme = "oidc";
});
    .AddCookie("Cookies")
    .AddOpenIdConnect("oidc", options =>
    {
        options.SignInScheme = "Cookies";

        options.Authority = "http://xxx.xxx.xxx.xxx:5000";
        options.RequireHttpsMetadata = false;

        options.ClientId = "foo";
        options.ClientSecret = "secret";
        options.ResponseType = "code id_token";

        options.SaveTokens = true;
        options.GetClaimsFromUserInfoEndpoint = true;

        options.Scope.Add("api1");
        options.Scope.Add("offline_access");
    });

我需要类似于我的 Startup.cs 文件的 ConfigureServices() 方法中的 IServiceCollection 服务参数的 AddAuthentication() 扩展,以便能够允许客户端通过 IdentityServer4 登录。

【问题讨论】:

  • 引用的代码遇到了什么问题?如果您在 Visual Studio 中本地运行 IdentityServer4,它会很好地记录其对输出窗口的请求处理。你能看到那里记录的任何错误吗?您的请求是否真正发送到身份服务器?
  • @jbeanky 上面的代码在 .Net core 2.0 项目中工作得非常好,但是我试图在 .Net Framework 4.6 项目中复制这个功能,这就是我遇到错误的地方. .Net Framework 中的 AddAuthentication 扩展方法使用 SharedAuthenticationOptions 类,而 .Net core 2.0 中的 AddAuthentication 方法使用 AuthenticationOptions 类。他们都有不同的 getter 和 setter,我不知道这是否是问题所在,我只需要让 OpenId Connect 与 .Net Framework 4.6 一起使用。
  • 查看此文档以将客户端配置为 IdentityServer3。它应该基本相同,因为两者都遵循相同的规范。 identityserver.github.io/Documentation/docsv2/overview/…

标签: c# .net asp.net-core-2.0 identityserver4 openid-connect


【解决方案1】:

在 .net 框架中,您可以使用 Startup.Auth.cs 文件中的 Microsoft.Owin.Security.OpenIdConnect 库配置 OpenID Connect,例如:

public void ConfigureAuth(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = Authority,
                PostLogoutRedirectUri = redirectUri,
                RedirectUri = redirectUri,

                Notifications = new OpenIdConnectAuthenticationNotifications()
                {
                    //
                    // If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
                    //
                    AuthenticationFailed = OnAuthenticationFailed
                }

            });
    }

    private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context)
    {
        context.HandleResponse();
        context.Response.Redirect("/Home/Error?message=" + context.Exception.Message);
        return Task.FromResult(0);
    }

【讨论】:

    【解决方案2】:

    感谢您的回复!我会说@Nan Yu 的答案可能最终与我想出的解决方案最接近,但我想我会在我的 Startup.cs 文件的 Configure() 方法中分享我最终得到的结果.

    using Microsoft.AspNetCore.Authentication.OpenIdConnect;
    using Microsoft.IdentityModel.Protocols.OpenIdConnect;
    ...
    var openidOptions = new OpenIdConnectOptions(authenticationScheme)
    {
        ClientSecret = secret,
        AutomaticAuthenticate = true,
        SignInScheme = "Identity.External",
        Authority = identityServerAddress,
        ClientId = clientId,
        RequireHttpsMetadata = true,
        ResponseType = OpenIdConnectResponseType.CodeIdToken,
        AutomaticChallenge= true,
        GetClaimsFromUserInfoEndpoint = true,
        SaveTokens = true,
        Events = new OpenIdConnectEvents
        {
            OnRemoteSignOut = async remoteSignOutContext =>
            {
                remoteSignOutContext.HttpContext.Session.Clear();
            },
        },
    };
    openidOptions.Scope.Clear();
    openidOptions.Scope.Add("openid");
    app.UseOpenIdConnectAuthentication(openidOptions);
    

    将它添加到我的 .NET Framework 4.6 客户端最终让我能够成功地与我的 .NET Core 2.0 身份服务器通信!我感谢所有试图提供帮助的人:)

    【讨论】:

      猜你喜欢
      • 2017-04-23
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 2018-12-04
      • 2010-09-08
      • 2013-09-21
      • 2011-05-14
      相关资源
      最近更新 更多