6

ASP.Net WebAPIアプリケーションでRedirectToActionを使用していますが、次の方法を試しました。

return RedirectToAction("AuthenticateUser", "AuthenticationServiceWebApi", new RouteValueDictionary
                                                                    {
                                                                        {"userName", model.UserName},
                                                                        {"password", model.Password}
                                                                    });

これにより、以下のようにリダイレクトが生成されます。

127.0.0.1:81/authenticationservicewebapi/authenticateuser/admin/admin@123

ただし、WebAPIを使用しているので、以下のようなURLにする必要があります。

127.0.0.1:81/api/authenticationservicewebapi/authenticateuser/admin/admin@123

どうすればよいですか?

4

3 に答える 3

5

ユーザーが認証されていない場合は、リダイレクトしないでください。通常、もう一方の端にはインタラクティブユーザーがいないため、リダイレクトではなくHTTPステータスコード401を返す必要があります。


ASP.NETWebAPIに相当するものはありません。

あなたがそれをすることを主張するならば、ここにそれがあります:

あなたが投げるHttpResponseException

var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.Found);
httpResponseMessage.Headers.Location = new Uri(myAddress, UriKind.Relative);
throw new HttpResponseException(httpResponseMessage);
于 2012-07-31T09:46:08.650 に答える
4

ルーティング、Web APIアクションシグネチャがどのように見えるかわからないので、推測してみます。ここでは実際には足りないものがいくつかあります(なぜURLでパスワードを渡すのですか?)

しかし...

あなたのURL構造を考えると、あなたのルーティングは次のようなものだと思います:

    routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/{id}/{id2}",
        defaults: new { id = RouteParameter.Optional, id2 = RouteParameter.Optional }
    );

それを考えると、あなたのauthenticateuserは次のようなものでなければならないと思います:

public HttpResponseMessage AuthenticateUser([FromUri]string id, [FromUri]string id2)

その場合、MVCコントローラーからこれにリダイレクトするには、次のものが必要です。

        return Redirect(
            Url.RouteUrl("DefaultApi", 
                new { httproute = "", 
                      controller = "AuthenticationServiceWebApi", 
                      action = "AuthenticateUser", 
                      id = model.UserName,
                      id2 = model.Password
            }));
于 2012-07-31T10:31:27.643 に答える
0

私も簡単な解決策を思いついた。上記ほど良くはありませんが、共有する価値があります。

        string post_data = "userName=th&password=admin@123";

        string uri = "http://127.0.0.1:81/api/authenticationservicewebapi/authenticateuser";

        // create a request
        HttpWebRequest request = (HttpWebRequest)
        WebRequest.Create(uri); 
        request.KeepAlive = false;
        request.ProtocolVersion = HttpVersion.Version10;
        request.Method = "POST";

        // turn our request string into a byte stream
        byte[] postBytes = Encoding.ASCII.GetBytes(post_data);

        // this is important - make sure you specify type this way
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = postBytes.Length;
        Stream requestStream = request.GetRequestStream();

        // now send it
        requestStream.Write(postBytes, 0, postBytes.Length);
        requestStream.Close();

皆さん、ありがとうございました。

于 2012-07-31T10:55:42.697 に答える