質問

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は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)
役に立ちましたか?

解決 3

私がやったのは、ステータスコードを削除して、 null を返すだけでした。ファイルがない場合、メソッドは合計ページカウントが0の空のリストを返します。しかし、ユーザーが認証されていない場合、 null を返します。そのような場合は window.locationを使用しますをクリックしてブラウザをログインページにリダイレクトします。

他のヒント

Webサービスからリダイレクトしないでください。リダイレクトするかどうかを示す値を返す必要があります(これは、 string を返す別の Authenticate メソッドである可能性があります。nullの場合は、認証済み、リダイレクトURLが含まれていない場合)その後、javascriptで window.location プロパティを設定することにより、戻り値を確認して適切にリダイレクトできます。

>

ところで、Webサービスへのアクセスには認証は必要ありません。

特にasp.netの経験はありませんが、一般に、「成功」、「コンテンツ」、「エラータイプ」などのプロパティを持つajaxResponseオブジェクトを使用するのが好きです。この場合、ログインが必要な応答は、「false」の成功となります。およびerrortype&quot; login&quot;または任意の選択。 javascriptコードは、これらのプロパティに基づいて返されたオブジェクトの処理方法を決定し、ユーザーにログインフォームを表示するか、新しいページにリダイレクトします。

Webサービスで例外をスローします。これを呼び出し元でキャッチし、必要に応じてリダイレクトします。

実際、ウェブサービスへの投稿(ajaxかどうか)に「Content-Type」が含まれている場合、値が&quot; application / json;のヘッダーcharset = utf-8&quot;、ASMXスタックは、ステータスコード401(無許可)を含む通常想定されるリダイレクトを除き、json形式の文字列を返しますが、コンテンツタイプヘッダーを送信しない場合、ステータスコードOKのレスポンスとしてログインページへのブラウザリダイレクトを取得し、ajaxを使用すると、レスポンスとしてログインページhtmlを取得します。

これが明確化に役立つことを願っています。

よろしく

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top