繁体   English   中英

没有显式登录的ASP.NET登录重定向

[英]ASP.NET logon redirect without explicit logon

带有MVC 3.0的ASP .NET 4.0

所以情况就是这样:我是MVC和ASP.NET的新手,我有一个MVC应用程序,在web.config中使用FormAuthentication和以下内容:

<authentication mode="Forms">
   <forms loginUrl="~/LogOn/LogOn" timeout="2" requireSSL="true" />
</authentication>

通常会发生的情况是,如果用户在会话过期后导航到某个页面,则会将其定向到LogOn页面,该页面正常工作。 我想要做的是阻止该页面被发送回来,如果请求是一个Ajax调用,而是发送一个JSON对象来指示失败。
我已经抓住了请求,通过以下方式检查XMLHttp类型:

void MvcApplication_AuthenticateRequest(object sender, EventArgs e)
{
        if (s_identityProvider == null)
            return;
        IClaimsPrincipal principal = GetClaimsPrincipal();
        Context.User = principal;
        if (principal != null)
            ClaimProvider.CheckAndPopulateRoles(principal);
        else
        {
            if (Context.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
            {
                Context.Response.Write( "<?xml version='1.0' encoding='ISO-8859-1'?>"+
                                        "<note>"+
                                        "</note>";
            }
            else
            {
                FormsAuthentication.SignOut();
            }
        }
}

现在,我已经测试过我对AJAX调用的检查似乎有效,唯一奇怪的是,在catch之后,登录页面仍然是作为响应发送的。 我已经检查了FormsAuthentication方法的所有用法,以确保没有其他人强制执行FormsAuthentication.RedirectToLoginPage()并检查,事实上,所有使用FormsAuthentication并且没有人做任何奇怪的事情。 我还检查了登录页面URL的查询(通过GetRedirectUrl() ),但仍然没有。

有没有人有什么想法做autoredirect?

我唯一的猜测是AuthenticateRequest方法不是这里Request的生命周期的结束。 其结果是,该Response被最终返回给用户是重定向响应。 在捕获并修改AJAX Response之后,应该尝试显式结束Response ,以避免MVC框架进一步操作

if (Context.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
    Context.Response.Write( "<?xml version='1.0' encoding='ISO-8859-1'?>"+
                            "<note>"+
                            "</note>";
    Context.Response.End(); // Calling End here should complete the response.
 }
 else {
            // ... other stuff
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM