Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET登录重定向而不显式登录_Asp.net_Ajax_Asp.net Mvc_Asp.net Ajax - Fatal编程技术网

ASP.NET登录重定向而不显式登录

ASP.NET登录重定向而不显式登录,asp.net,ajax,asp.net-mvc,asp.net-ajax,Asp.net,Ajax,Asp.net Mvc,Asp.net Ajax,ASP.NET4.0与MVC3.0 情况是这样的:我不熟悉MVC和ASP.NET,我有一个MVC应用程序,它在web.config中使用FormAuthentication进行以下验证: <authentication mode="Forms"> <forms loginUrl="~/LogOn/LogOn" timeout="2" requireSSL="true" /> </authentication> 通常发生的情况是,如果用户在会话过期后

ASP.NET4.0与MVC3.0

情况是这样的:我不熟悉MVC和ASP.NET,我有一个MVC应用程序,它在web.config中使用FormAuthentication进行以下验证:

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

通常发生的情况是,如果用户在会话过期后导航到某个页面,他们将被定向到登录页面,这样可以正常工作。如果请求是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();
            }
        }
}
void mvcapapplication\u AuthenticateRequest(对象发送方,事件参数e)
{
如果(s_identityProvider==null)
返回;
IClaimsPrincipal=GetClaimsPrincipal();
Context.User=principal;
if(主体!=null)
课程改进、检查和推广(负责人);
其他的
{
if(Context.Request.Headers[“X-Requested-With”]=“XMLHttpRequest”)
{
Context.Response.Write(“”)+
""+
"";
}
其他的
{
FormsAuthentication.SignOut();
}
}
}
现在,我已经测试了我对AJAX调用的检查是否有效,唯一奇怪的是在捕获之后,登录页面仍然作为响应发送。我已经检查了FormsAuthentication方法的所有用法,以确保没有其他人强制执行
FormsAuthentication.RedirectToLoginPage()
并检查,事实上,FormsAuthentication的所有用法都没有做任何奇怪的事情。我还检查了登录页面URL的查询(通过
GetRedirectUrl()
),但仍然没有任何结果。


有人知道自动重定向将做什么吗?

我唯一的猜测是,
AuthenticateRequest
方法不是请求生命周期的终点。因此,最终返回给用户的
响应是重定向响应。您应该尝试显式结束
响应捕获并修改AJAX响应以避免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
 }
if(Context.Request.Headers[“X-Request-With”]=“XMLHttpRequest”)
{
Context.Response.Write(“”)+
""+
"";
Context.Response.End();//在此处调用End应该完成响应。
}
否则{
//…其他东西
}

太好了,就这样。非常感谢。