代码之家  ›  专栏  ›  技术社区  ›  intrepidus

在自定义登录页面上使用现有cookie身份验证的身份状态

  •  1
  • intrepidus  · 技术社区  · 10 年前

    我的任务是将IDS3集成到现有的遗留MVC应用程序中,我将其迁移到使用CookieAuthentication的OWIN。通过这个很棒的示例项目,我成功地获得了一个简单的设置,它可以使用自定义用户服务、自定义登录页面和代码流测试客户端。

    我现在正在尝试解决这个问题:如果用户已经通过现有的cookie身份验证登录,并通过我们的测试客户端启动了一个代码流,那么自动将他们登录到IDS3,这样他们就不会被提示再次输入凭据。下面是不工作的代码来显示我的思维过程:

    [Route("identity/logintest", Name = "ids3-login")]
    public ActionResult IdsLogin(string id)
    {
        var ctx = Request.GetOwinContext();
        var user = ctx.Authentication.User;
    
        // If they're already logged in via cookie auth, automatically
        // log them in to IDS3 and send them on their way
        if (user.Identity.IsAuthenticated)
        {
            var env = ctx.Environment;
    
            env.IssueLoginCookie(new IdentityServer3.Core.Models.AuthenticatedLogin
            {
                Subject = User.Identity.Name,
                Name = User.Identity.Name,
            });
    
            var msg = env.GetSignInMessage(id);
            var returnUrl = msg.ReturnUrl;
    
            env.RemovePartialLoginCookie();
    
            return Redirect(returnUrl);
        }
    
        // Otherwise show the login form as usual
        return View();
    }
    

    如果我通过cookie身份验证以用户身份登录,则为user的值。标识未填充该信息,因此IsAuthenticated为false。我想我对当前失败的原因有了一个伪的理解:我要求的是与IDS3相关联的上下文的Authentication值,而不是我的MVC应用程序的Autheurication值。(这在概念上很混乱,因为该控制器是我的MVC应用程序的一部分。)

    这可能不是一个IDS3问题,而是一个OWIN问题,但我希望有人以前尝试过实现这种黑客方法,并能为我指明正确的方向。希望当我浏览了我在上找到的所有OWIN样本时 http://www.asp.net/aspnet/samples/owin-katana 事情会变得更有意义,但现在我被卡住了。

    启动。cs供参考(不包括自定义用户服务代码,因为它基本上是从CustomLoginPage示例项目复制粘贴的):

    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var container = AutofacConfig.Configure();
    
            app.UseAutofacMiddleware(container);
            app.UseAutofacMvc();
    
            ConfigureAuthentication(app);
            ConfigureIdentityServer(app);
        }
    
        private static void ConfigureAuthentication(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
                LoginPath = new PathString("/login")
            });
        }
    
        private static void ConfigureIdentityServer(IAppBuilder app)
        {
            var factory = new IdentityServerServiceFactory()
                .UseInMemoryClients(Clients.Get())
                .UseInMemoryScopes(StandardScopes.All);
    
            factory.UserService = new Registration<IUserService>(resolver =>
                resolver.ResolveFromAutofacOwinLifetimeScope<IUserService>());
    
            var options = new IdentityServerOptions
            {
                SiteName = "test",
                SigningCertificate = LoadCertificate(),
                Factory = factory,
    
                AuthenticationOptions = new AuthenticationOptions
                {
                    EnableLocalLogin = true,
    
                }
            }
    
            app.Map("/identity", idsrvApp =>
            {
                idsrvApp.UseIdentityServer(options);
            });
        }
    }
    
    2 回复  |  直到 10 年前
        1
  •  0
  •   Brock Allen    10 年前

    如果用户已经通过现有的cookie身份验证登录,并通过我们的测试客户端启动代码流,则自动将他们登录到IDS3,这样他们就不会被提示再次输入凭据

    如果您实施 PreAuthenticate 在您的自定义用户服务中,您可以检测您的自定义cookie,以知道用户已通过身份验证。然后,您可以发出有效的 AuthenticationResult 从…起 预身份验证 .我们有一个样本显示了类似的情况。

    我不确定你上面的代码的其余部分——我对你为什么拥有它以及它在哪里适合感到有些困惑,所以我不太愿意评论为什么它可能工作或者不工作。

        2
  •  0
  •   Lukas K    10 年前

    很高兴看到有人需要和我一样的黑客解决方案。我不得不使用旧的会员cookie来使用IdentityServer认证用户。我的解决方案非常简单-我没有使用 用户身份验证 但是直接从Response读取cookie值作为原始值,并使用Membership类从中解密用户名。通过这一点,我在使用不同的cookie提供程序进行身份验证时没有遇到问题。由于整个概念都很糟糕,所以我不介意在这种情况下手动读取cookie。