ASP MVC单应用多
我有以下客户特别要求的严格方案:使用Asp.NET MVC4的单个网站,可通过各种域使用单点登录机制访问。
通过在webconfig中指定二级域名,我已设法通过子域名进行表单身份验证
<authentication mode="Forms">
<forms name="SingleSignOn" loginUrl="/Login/LoginRedirect" timeout="10" slidingExpiration="false" domain="domain.ml" cookieless="UseCookies" enableCrossAppRedirects="true">
<credentials passwordFormat="SHA1" />
</forms>
</authentication>
另外,当在登录逻辑中调用FormsAuthentication.SetAuthCookie
时,我还指定了第二级域:
System.Web.HttpCookie MyCookie = System.Web.Security.FormsAuthentication.GetAuthCookie(lName, false);
MyCookie.Domain = lSecondLevelDomain;
FormsAuthentication.SetAuthCookie(lName, false);
在不同的域中,这不起作用,因为实际的域不会与web.config中指定的域匹配,也不会与cookie匹配。
目标是:
用户访问domain1.com用户重定向到logindomain.com并创建经过身份验证的Cookie用户重定向回domain1.com
用户总是被重定向到“登录域”,cookie是使用该域创建的,并且始终使用跨域的相同cookie进行身份验证。
是否可以重写Authorize属性的逻辑,以便允许使用登录域的cookie而不是用户最初使用的域进行授权?
在深入编程之前,请看看SO的新自动登录功能是如何工作的? 了解如何实施这样的场景。
然后查看适用于跨域ASP.NET应用程序的跨应用程序的Forms身份验证和单一登录(SSO)。 现在你可以达到你的目的,只要你想:)
如果您强烈考虑生成的绝对返回网址的有效性,您还可以使用以下代码:
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);
// or use context.Request.PathBase + context.Request.Path + context.Request.QueryString
}
context.Response.Redirect(context.RedirectUri);
}
}
链接地址: http://www.djcxy.com/p/21679.html