我的任务是将IDS3集成到我迁移到使用CookieAuthentication的OWIN的现有遗留MVC应用程序中。通过令人敬畏的示例项目,我设法使用自定义用户服务,自定义登录页面和代码流测试客户端进行简单设置。
我现在正在尝试解决此要求:如果用户已通过现有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 auth以用户身份登录,则user.Identity的值不会填充该信息,因此IsAuthenticated为false。我认为我对当前失败的原因有一个伪理解:我要求与IDS3相关联的上下文的身份验证值,而不是我的MVC应用程序具有我认证的cookie身份验证的值。 (这在概念上令人困惑,因为该控制器是我的MVC应用程序的一部分。)
这可能不是一个IDS3问题,更多的是OWIN问题,但我希望其他人之前试图实施这种hacky方法,并指出我正确的方向。希望当我浏览http://www.asp.net/aspnet/samples/owin-katana时发现的所有OWIN样本时,事情会更有意义,但是现在我被卡住了。
Startup.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);
});
}
}
答案 0 :(得分:0)
如果用户已通过现有cookie身份验证登录并通过我们的测试客户端启动代码流,则会自动将其登录到IDS3,以便不再提示他们再次输入凭据
如果您在自定义用户服务中实施PreAuthenticate
,那么您可以检测自定义Cookie以了解用户已经过身份验证。然后,您可以从AuthenticationResult
发出有效的PreAuthenticate
。我们有一个样本显示这样的东西。
我不确定你上面的其他代码 - 我有点困惑你为什么拥有它以及它适合的地方,所以我不愿评论为什么它可能会也可能不会起作用。
答案 1 :(得分:0)
很高兴看到有人需要和我一样的hacky解决方案。我不得不使用旧的Membership cookie来验证IdentityServer用户。我的解决方案非常简单 - 我没有使用 user.Identity.IsAuthenticated ,但是直接从Response中读取来自Response的cookie值,并使用Membership类来解密用户名。通过这个,我没有遇到与不同cookie提供商进行身份验证的问题。因为整个概念都是hacky无论如何我都不介意在这种情况下手动读取cookie。