Raf*_*afe 16 asp.net asp.net-mvc owin asp.net-identity
在visual studio中创建新的Web应用程序(webforms或mvc)时,会有后验证逻辑,它会检查查询字符串中的ReturnUrl参数,然后将用户重定向到那里:如果它存在:在weforms中 - Login.aspx .cs你有这个:
protected void LogIn(object sender, EventArgs e)
{
...
switch (result)
{
case SignInStatus.Success:
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
break;
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
在MVC - AccountController.cs中你有这个:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
...
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
...
}
}
Run Code Online (Sandbox Code Playgroud)
此外,Owin配置设置为使用cookie身份验证,该身份验证使用Microsoft.Owin.Security.Cookies.CookieAuthenticationHandler类,该类本身检查ReturnUrl参数,并应用重定向(如果存在):
protected override async Task ApplyResponseGrantAsync()
{
AuthenticationResponseGrant signin = Helper.LookupSignIn(Options.AuthenticationType);
bool shouldSignin = signin != null;
AuthenticationResponseRevoke signout = Helper.LookupSignOut(Options.AuthenticationType, Options.AuthenticationMode);
bool shouldSignout = signout != null;
if (shouldSignin || shouldSignout || _shouldRenew)
{
...
if ((shouldLoginRedirect || shouldLogoutRedirect) && Response.StatusCode == 200)
{
IReadableStringCollection query = Request.Query;
string redirectUri = query.Get(Options.ReturnUrlParameter);
if (!string.IsNullOrWhiteSpace(redirectUri)
&& IsHostRelative(redirectUri))
{
var redirectContext = new CookieApplyRedirectContext(Context, Options, redirectUri);
Options.Provider.ApplyRedirect(redirectContext);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
两个重定向似乎在登录/身份验证请求期间执行.一个应用于HttpContext.Response,另一个应用于Owin重定向上下文.根据我的经验,它看起来像后者重定向呼叫胜,这就是如果你有特殊的重定向逻辑在网站代码中应用后登录,因为它被通过内置Owin重定向覆盖的问题.
这种重复逻辑有充分的理由吗?这只是糟糕的设计吗?所以,如果我使用asp.net Owin CookieAuthentication,我应该在帐户控制器或aspx代码后面有登录后重定向代码逻辑吗?如果是这样,重定向应该应用于HttpContext.Response还是通过Owin以某种方式应用?
正如你所说,这三种重定向方式都属于不同的部分:WebForms、MVC、OWIN。它们中的每一个都可以独立使用(在自托管情况下为 OWIN),因此需要在每一个中执行相同的操作。
但是我不完全确定为什么最新的 MVC 模板需要RedirectToLocal. 我会选择向后兼容性——这种方法已经存在很长时间了。
此外,OWIN 重定向在 MVC 中也不起作用 - 在我的一个应用程序中,我总是根据用户的角色重定向用户,即使有一个带有本地 URL 的参数可供访问,我的用户始终会到达 MVC 控制器中指定的页面。
然而,仔细查看 OWIN 源代码和重定向逻辑后,MVC 获胜似乎很奇怪。可能需要一路单步执行,看看 MVC 场景中会发生什么。
| 归档时间: |
|
| 查看次数: |
1799 次 |
| 最近记录: |