与另一个MVC 5 Identity网站进行ASP.Net MVC 5身份验证

时间:2014-08-19 06:35:53

标签: asp.net authentication asp.net-mvc-5 asp.net-identity owin

我创建了两个MVC 5网站。第一个是Identity,我在其中启用了DB,Facebook和Google身份验证。现在我有第二个空的MVC 5站点,我想重定向到第一个站点进行身份验证。如何将身份验证重定向到第一个站点?第二个网站的所有其他方面应该作为标准,如果我用[Authorize]等装饰一些东西。

注意:这些可以属于不同的域。例如; mail.live.com 转到 login.live.com 进行身份验证。

有没有办法创建自己的OWIN身份验证提供程序,如Microsoft为Facebook / Google提供的?

任何指向右边的指针都会有所帮助。

1 个答案:

答案 0 :(得分:0)

  

有没有办法创建自己的OWIN身份验证提供程序,如   微软提供Facebook /谷歌?

,这是可能的。考虑到安全性很难,因此您需要花费大量时间来编写干净的代码。您需要充分了解开放式身份验证技术。请参阅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);
    }
}