ASP.Net MVC 5 身份验证与另一个 MVC 5 身份网站
Posted
技术标签:
【中文标题】ASP.Net MVC 5 身份验证与另一个 MVC 5 身份网站【英文标题】:ASP.Net MVC 5 authentication with another MVC 5 Identity website 【发布时间】:2014-10-12 04:15:22 【问题描述】:我已经创建了两个 MVC 5 网站。第一个是 Identity,我启用了 DB、Facebook 和 Google 身份验证。现在我有第二个空的 MVC 5 站点,我想重定向到第一个站点进行身份验证。如何将身份验证重定向到第一个站点?第二个网站的所有其他方面都应该按照标准工作,比如我用[Authorize]
等装饰东西。
注意:这些可以属于不同的域。例如; mail.live.com 转到 login.live.com 进行身份验证。
有没有办法像微软为 Facebook/Google 提供的那样创建自己的 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);
【讨论】:
以上是关于ASP.Net MVC 5 身份验证与另一个 MVC 5 身份网站的主要内容,如果未能解决你的问题,请参考以下文章
带有 ASP.NET MVC 5 应用程序的 Microsoft 身份验证循环
使用 ASP.NET 5 MVC 6 Web API 进行 Cookie 身份验证
我们可以在不使用 ASP.Net Identity 的情况下使用 MVC 5 提供的 cookie 身份验证吗?