我正在使用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;
}重定向不起作用,执行此操作时,我总是收到500内部服务器错误。我试过不同的代码。去这里的推荐方式是什么?我需要从JavaScript读取重定向信息并加载新页面(或者以AJAX方式显示登录控件)。
我实际上得到了一个JSON对象,它看起来像这样:
{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"
}我尝试运行调试器,但没有显示输入了任何方法。如您所见,StackTrace为空...
当在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)发布于 2009-03-06 07:46:21
我所做的就是去掉状态码,只返回null。如果没有文件,则方法返回一个空列表,总页数为0。但是,如果用户未通过身份验证,它将返回null,在这种情况下,我使用window.location将浏览器重定向到登录页面。
发布于 2009-03-04 14:07:03
您不应该从Web服务重定向。它应该返回一个值来指示您是否应该重定向(这可以是一个返回string__的单独Authenticate方法,如果它为null,则表示它已通过身份验证,如果不是,它将包含重定向URL),然后在javascript中,您可以检查返回值,并通过设置window.location属性进行适当的重定向。
顺便说一句,对Web服务的访问应该不需要身份验证。
发布于 2009-03-04 14:21:35
我没有特别使用asp.net的经验,但通常我喜欢使用具有“成功”、“内容”、“错误类型”等属性的ajaxResponse对象。在这种情况下,需要登录的响应的成功值为"false“,错误类型为" login ",或者您选择的任何值。然后,javascript代码根据这些属性决定如何处理返回的对象,向用户显示登录表单或重定向到新页面。
https://stackoverflow.com/questions/610699
复制相似问题