【问题标题】:OpenId Connect middleware not setting auth cookie in an WebForms appOpenId Connect 中间件未在 WebForms 应用程序中设置身份验证 cookie
【发布时间】:2015-12-24 01:20:45
【问题描述】:

我正在尝试将 OpenId Connect 集成到长期存在的网络表单应用程序中。我能够迁移应用程序以使用 OWIN,并且我正在使用 OpenIdConnectAuthenticationMiddleware 对我的 IdP 提供商进行身份验证。一切都很好,直到我需要构建从 IdP 获得的新身份并设置 cookie - 我认为这部分没有发生。

我的 Startup.Configure 方法的重要部分:

app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/login.aspx"),
    CookieManager = new SystemWebCookieManager() //custom cookie manager
});


app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
    Authority = "https://[development_domain]/core",
    ClientId = "VDWeb",
    ResponseType = "code id_token token",
    Scope = "openid profile",
    UseTokenLifetime = true,
    SignInAsAuthenticationType = "Cookies",
    Notifications = new OpenIdConnectAuthenticationNotifications
    {
        SecurityTokenValidated = async n =>
        {
            var userInfo = await EndpointAndTokenHelper.CallUserInfoEndpoint(n.ProtocolMessage.AccessToken);

            //now store Preferred name :
            var prefNameClaim = new Claim(
                Thinktecture.IdentityModel.Client.JwtClaimTypes.PreferredUserName,
                userInfo.Value<string>("preferred_username"));

            var myIdentity = new ClaimsIdentity(
                n.AuthenticationTicket.Identity.AuthenticationType,
                Thinktecture.IdentityModel.Client.JwtClaimTypes.PreferredUserName,
                Thinktecture.IdentityModel.Client.JwtClaimTypes.Role);

            myIdentity.AddClaim(prefNameClaim);

            //add unique_user_key claim
            var subjectClaim = n.AuthenticationTicket.Identity.FindFirst(Thinktecture.IdentityModel.Client.JwtClaimTypes.Subject);

            myIdentity.AddClaim(new Claim("unique_user_key", subjectClaim.Value));
            myIdentity.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken));

            var ticket = new AuthenticationTicket(myIdentity, n.AuthenticationTicket.Properties);
            var currentUtc = new SystemClock().UtcNow;
            ticket.Properties.IssuedUtc = currentUtc;
            ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromHours(12));
            n.AuthenticationTicket = ticket;                
        },
    }
});

我可以确认 AuthentocationTicket 已正确填充,但未设置身份验证 cookie。我确实知道这个问题https://katanaproject.codeplex.com/workitem/197,并且我已经尝试了为这个问题提供的所有解决方法,但没有任何帮助。有趣的是,当我尝试将自己的 cookie 放入 SecurityTokenValidated 事件 - n.Response.Cookies.Append("Test", "Test"); 时,我可以看到 cookie 设置正确。

其中一种解决方法建议实现您自己的 CookieManager。让我好奇的是,当我在这个自定义管理器中的 cookie 设置器中设置断点时,它没有被命中,即中间件似乎甚至没有尝试设置 cookie。所以我的主要问题 - 中间件究竟会在什么时候尝试设置 cookie?是在我设置 AuthenticationTicket 的时候吗?

编辑 1: 添加更多信息。我尝试与另一个 Web 应用程序进行比较,这次是 MVC,我配置为使用相同的 IdP 并且按预期工作。两个应用程序的启动代码相同。通过 SecurityTokenValidated 事件进行调试时,我可以看到 MVC 应用程序(工作)创建了 System.Security.Principal.WindowsPrincipal 身份,而 webforms 应用程序(非工作)创建了 System.Security.Principal.GenericIdentity 身份。

我还添加了这个小片段

app.UseStageMarker(PipelineStage.Authenticate);
app.Use((context, next) =>
{
    var identity = context.Request.User.Identity;
    return next.Invoke();
});

只是想看看在这个管道阶段填充了什么身份。对于 MVC 应用程序(工作),我看到了通过设置 AuthenticationTicket 添加的身份,对于 webforms 应用程序,我仍然看到未经身份验证的 GenericIdentity。

【问题讨论】:

    标签: authentication cookies owin middleware openid-connect


    【解决方案1】:

    好的,这很尴尬 - 问题出在 CookieAuthenticationOptions 中,显然AuthenticationType = DefaultAuthenticationTypes.ApplicationCookieAuthenticationType = "Cookies"相同的。稍后设置后,它就可以正常工作了。

    【讨论】:

    • 您是否为您的实施使用了特定的解决方案?我正在尝试做类似的事情,但我很难弄清楚 OpenID Connect 上的代码示例从什么开始。
    • 2 天把我的头撞在砖墙上。先生,您是我的救星。
    【解决方案2】:

    您可以使用默认的 cookie 管理器并查看是否会设置 cookie 吗?

    【讨论】:

    • 您好,尝试使用默认 cookie 管理器。 Auth cookie 仍然 not 设置,测试 cookie(根据我原来的问题)is 设置。
    猜你喜欢
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    • 2016-06-28
    • 2020-04-17
    • 2021-04-04
    • 2013-09-16
    • 1970-01-01
    相关资源
    最近更新 更多