【问题标题】:ASP.Net MVC 5 authentication with another MVC 5 Identity websiteASP.Net MVC 5 身份验证与另一个 MVC 5 身份网站
【发布时间】:2014-10-12 04:15:22
【问题描述】:
我已经创建了两个 MVC 5 网站。第一个是 Identity,我启用了 DB、Facebook 和 Google 身份验证。现在我有第二个空的 MVC 5 站点,我想重定向到第一个站点进行身份验证。如何将身份验证重定向到第一个站点?第二个网站的所有其他方面都应该按照标准工作,比如我用[Authorize] 等装饰东西。
注意:这些可以属于不同的域。例如; mail.live.com 转到 login.live.com 进行身份验证。
有没有办法像微软为 Facebook/Google 提供的那样创建自己的 OWIN 身份验证提供程序?
任何指向右侧的指针都会有所帮助。
【问题讨论】:
标签:
asp.net
authentication
asp.net-mvc-5
asp.net-identity
owin
【解决方案1】:
有没有办法创建自己的 OWIN 身份验证提供程序,例如
微软为 Facebook/Google 提供什么?
是的,这是可能的。考虑到安全性很难,所以需要花费大量时间来编写干净的代码。您需要对开放式身份验证技术有很好的了解。请看How does SO's new auto-login feature work?了解我的意思。
您还可以更改身份验证策略以简化问题。看看Single Sign On (SSO) for cross-domain ASP.NET applications。
如果您强烈考虑生成的绝对返回 URL 的有效性,您可以使用以下代码:
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://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);
}
}