Dan*_*Dan 1 asp.net webforms formsauthentication owin
我需要使用表单身份验证将 Facebook(以及后来的 google 等)身份验证添加到现有的 asp.net webforms 应用程序。
我快到了,但表单身份验证和 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();
Run Code Online (Sandbox Code Playgroud)
如果我关闭表单身份验证,这会起作用。如果我有表单身份验证,用户将被重定向到 web.config 中定义的表单身份验证 URL:
<authentication mode="Forms">
<forms name=".ASPXAUTH_Test" loginUrl="~/start/login.aspx" timeout="60" >
</forms>
</authentication>
Run Code Online (Sandbox Code Playgroud)
根据我的理解,Http 状态代码 (401) 会触发重定向到 web.config 中的 Url。我试图设置其他状态代码,但它们根本不起作用。当我在 web.config 中关闭表单身份验证时,实际的登录过程仍然有效(令人惊讶)但是如果我在未登录的情况下访问受保护的页面,则会得到一个丑陋的 IIS 错误页面,而不是被重定向。
看来,我无法获得工作表单身份验证和外部身份验证才能正常工作:-(
到目前为止,所有替代方案对我来说似乎都不诱人: - 切换到身份框架(在我们的特定环境中,这绝对不是一个选项。我只是为了完整性才提到它) - 尝试使用 web.api 或其他东西类似(可能有同样的问题) - 放弃 owin 外部身份验证并手动实现所有内容
有没有人能够完成这项工作?任何帮助表示赞赏。提前致谢。
Halleluja, I found the solution:
From .Net 4.5 it is possible to prevent the forms redirect in the response: Response.SuppressFormsAuthenticationRedirect = true;
so the working code would look like:
var owinContext = Context.GetOwinContext();
owinContext.Authentication.Challenge(properties, provider);
Response.SuppressFormsAuthenticationRedirect = true;
Response.StatusCode = 401;
Response.End();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
632 次 |
| 最近记录: |