如何在AJAX调用中从Web服务重定向到登录页面?

Paw*_*iak 0 asp.net authentication ajax redirect web-services

我使用jQuery来调用Web服务(*.asmx)方法.Web服务使用FormsAuthentication来确定是否对调用用户进行了身份验证.我无法从Web方法返回重定向状态代码,例如

[WebMethod(EnableSession=true)]
public Dictionary<string, object> GetArchivedFiles(int pageSize, int page)
{
    if(HttpContext.Current.User.Identity.IsAuthenticated && Session["UserId"] != null)
        // Do some computing and return a Dictionary.       

    // Method will fall through here if the user is not authenticated.
    HttpContext.Current.Response.StatusCode = (int) HttpStatusCode.Unauthorized;
    return null;
}
Run Code Online (Sandbox Code Playgroud)

重定向不起作用,执行此操作时我总是得到500内部服务器错误.我尝试了不同的代码.什么是推荐的方式去这里?我需要从JavaScript读取重定向信息并加载新页面(或者可能显示登录控件AJAX方式).

我实际上得到了一个JSON对象,看起来像这样:

{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"
}
Run Code Online (Sandbox Code Playgroud)

我尝试运行调试器,但它没有显示输入任何方法.如您所见,StackTrace为null ...

当在Fiddler中作为标准POST请求(而不是XMLHttpRequest)调用时,它实际上返回一个异常:

HTTP/1.1 500 Internal Server Error
Server: ASP.NET Development Server/9.0.0.0
Date: Wed, 04 Mar 2009 14:46:02 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Content-Length: 1739
Connection: Close

System.NotSupportedException: The type System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] is not supported because it implements IDictionary.
   at System.Xml.Serialization.TypeScope.GetDefaultIndexer(Type type, String memberInfo)
   at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference)
   at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
   at System.Xml.Serialization.ModelScope.GetTypeModel(Type type, Boolean directReference)
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root)
   at System.Web.Services.Protocols.XmlReturn.GetInitializers(LogicalMethodInfo[] methodInfos)
   at System.Web.Services.Protocols.XmlReturnWriter.GetInitializers(LogicalMethodInfo[] methodInfos)
   at System.Web.Services.Protocols.MimeFormatter.GetInitializers(Type type, LogicalMethodInfo[] methodInfos)
   at System.Web.Services.Protocols.HttpServerType..ctor(Type type)
   at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
   at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
Run Code Online (Sandbox Code Playgroud)

Meh*_*ari 7

您不应该从Web服务重定向.它应该返回一个值来指示你是否应该重定向(这可能是一个Authenticate返回a 的单独方法string,如果它为null,则表示它已经过身份验证,如果不是它将包含重定向URL)则在javascript中,您可以通过设置window.location属性来检查返回值并适当地重定向.

顺便说一下,访问Web服务不应该要求身份验证.