【问题标题】:Formsauthentication and owin facebook authentication side by side (web forms)Formsauthentication 和 owin facebook 身份验证并排(Web 表单)
【发布时间】:2016-09-06 17:37:50
【问题描述】:

我需要使用表单身份验证将 Facebook(以及后来的 google 等)身份验证添加到现有的 asp.net webforms 应用程序。

我快到了,但formsauthentication 和owin 身份验证之间似乎存在冲突。我的解决方案基于标准的 VS 2015 模板。

重定向到外部认证提供者(facebook)的相关代码如下:

string redirectUrl =
                ResolveUrl(String.Format(CultureInfo.InvariantCulture,
                    "~/Account/RegisterExternalLogin?{0}={1}&returnUrl={2}", IdentityHelper.ProviderNameKey,
                    provider, ReturnUrl));
            var properties = new AuthenticationProperties { RedirectUri = redirectUrl };

            Context.GetOwinContext().Authentication.Challenge(properties, provider);
            Response.StatusCode = 401;
            Response.End();

如果我关闭表单身份验证,这将有效。如果我有表单身份验证,用户将被重定向到 web.config 中定义的表单身份验证 URL:

    <authentication mode="Forms">
      <forms name=".ASPXAUTH_Test" loginUrl="~/start/login.aspx" timeout="60" >
        </forms>
    </authentication>

据我了解,Http 状态代码 (401) 会触发重定向到 web.config 中的 Url。我尝试设置其他状态代码,但它们根本不起作用。 当我在 web.config 中关闭表单身份验证时,实际的登录过程仍然有效(令人惊讶),但是如果我在没有登录的情况下访问受保护的页面,则会得到一个丑陋的 IIS 错误页面,而不是被重定向。

看来,我无法让工作表单身份验证和外部身份验证一起正常工作:-(

到目前为止,所有替代方案对我来说似乎都没有吸引力: - 切换到身份框架(在我们的特定环境中,这绝对不是一个选项。我只是为了完整性而提到它) - 尝试使用 web.api 或类似的东西(可能有同样的问题) - 放弃外部身份验证,手动实现一切

有没有人能够完成这项工作?任何帮助表示赞赏。提前致谢。

【问题讨论】:

    标签: asp.net webforms owin formsauthentication


    【解决方案1】:

    哈利路亚,我找到了解决办法:

    从 .Net 4.5 开始,可以防止表单在响应中重定向: Response.SuppressFormsAuthenticationRedirect = true;

    所以工作代码看起来像:

                var owinContext = Context.GetOwinContext();
                owinContext.Authentication.Challenge(properties, provider);
                Response.SuppressFormsAuthenticationRedirect = true;
                Response.StatusCode = 401;
                Response.End();
    

    【讨论】:

    • 我差点忘了给这个完美的作品点赞。任何其他建议的方法都不会,例如更改项目配置、制作虚拟目录等。我正要开始制作一个新项目,只是为了处理一个简单的 Microsoft 登录。谢谢!
    猜你喜欢
    • 2013-09-20
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多