我的任务是将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);
});
}
}