login页面在不同的域

我对OWINauthentication是完全陌生的,而且我一定是误解了一切工作,但是在任何地方我都找不到这个。

我想要的只是能够使用中央域进行身份validation。 如果有人在未经身份validation的情况下尝试访问apps.domain.com ,则会将其redirect到accounts.domain.com/login以便将所有身份validation分为自己的域和应用程序。 使用MVC 4表单身份validation非常简单,您可以指定完整的URL,但似乎并不在OWIN中。

Startup.Auth.cs

 app.UseCookieAuthentication(new CookieAuthenticationOptions { LoginPath = new PathString("/account/login") } 

使用CookieDomain选项设置Cookie时,指定域名很容易。 但是,当您指定要redirect到的loginpath时,必须相对于当前的应用程序,那么如何在MVC 4表单身份validation中完成如此简单的操作呢?

没有深入到OWINauthentication是什么,在search几个小时后,我找不到任何解决这个问题的东西。

 public class Startup { public void Configuration(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationMode = AuthenticationMode.Active, LoginPath = new PathString("/account/login"), LogoutPath = new PathString("/account/logout"), Provider = new CookieAuthenticationProvider { OnApplyRedirect = ApplyRedirect }, }); } private static void ApplyRedirect(CookieApplyRedirectContext context) { Uri absoluteUri; if (Uri.TryCreate(context.RedirectUri, UriKind.Absolute, out absoluteUri)) { var path = PathString.FromUriComponent(absoluteUri); if (path == context.OwinContext.Request.PathBase + context.Options.LoginPath) { context.RedirectUri = "http://accounts.domain.com/login" + new QueryString( context.Options.ReturnUrlParameter, context.Request.Uri.AbsoluteUri); } } context.Response.Redirect(context.RedirectUri); } } 

如果apps.domain.com是唯一可能的返回url,则应该强烈考虑将context.Request.Uri.AbsoluteUrireplace为context.Request.PathBase + context.Request.Path + context.Request.QueryString并构build绝对返回url在您的authentication服务器,以保护您的应用程序免受滥用redirect。

希望这可以帮助 ;)

编辑 :你可能会问自己,为什么我不直接应用使用context.RedirectUri属性的redirect。 事实上, ICookieAuthenticationProvider.ApplyRedirect负责多个redirect,对应于login和注销stream程(我知道,它打破了单一职责原则…)。 但更糟糕的是: context.RedirectUri可以在cookie被有效地发送回去的时候,在loginstream程的开始或最终浏览器的目的地(即真正的相对“返回URL”)中表示authentication端点的绝对URL浏览器…这就是为什么我们需要确保context.RedirectUri是绝对的,并对应于注册的context.Options.LoginPath

我正在通过https://github.com/IdentityServer/IdentityServer3的例子,我有一个不同的答案。; 在https://www.scottbrady91.com/Identity-Server/Identity-Server-3-Standalone-Implementation-Part-2的示例中,他们展示了一个使用独立IdP和Cookie身份validation的MVC应用程序。; 这个例子并没有包括让401redirect工作,但我偶然发现了一个方法。

基本的scheme是在AccountController中创build一个用于login的操作。

 public ActionResult SignIn() { // set up some bookkeeping and construct the URL to the central auth service return Redirect(authURL); } 

现在你有一个可以在启动中使用的本地URL

 public class Startup { public void Configuration(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = "Cookies", LoginPath = new PathString("/Account/SignIn") }); } 

另外还有一个额外的好处,就是你可以在菜单栏上为要login的用户设置一个操作链接,以便在401之前login。我们在这里所做的是分离决定如何做未经authentication的用户从获得authentication的方式请求资源。