2

WebApi を介してローカル ページと外部クライアントの両方にいくつかのサービスを提供する MVC ベースのサイトを作成しようとしているため、JSONP で同じオリジン ポリシー エラーを回避する必要があります。問題は、サイトが基本認証を使用していることです。ここの他の投稿から学んだように、JSONP では再生できません。投稿で提案されているように、user:pass を URL に挿入しようとしましたHow do I make a JSONP call with JQuery with Basic Authentication?、しかしこれは機能せず、サーバーは無許可のコードを返します。また、ブラウザーにユーザー名とパスワードを入力しても問題ないため、注入せずに呼び出しを試みました。ブラウザーは期待どおりに資格情報を要求しますが、何らかの理由で拒否され、再び不正なコードに終わります. それでも、同じドメインからまったく同じコードを正常に実行することで確認できるため、資格情報は問題ありません。私のコードの何が問題なのか誰か教えてもらえますか?

私のMVC WebApiコントローラーアクションは次のようなものです:

[BasicAuthorize(Roles = "administrator,customer,trial")]
public class TextApiController : ApiController
{
      // ...

    public SomeResult Get([FromUri] SomeParams p)
    {
          // some processing which returns a SomeResult object
          //...
    }
}

BasicAuthorize 属性は、次のようにhttp://kevin-junghans.blogspot.it/2013/02/mixing-forms-authentication-basic.htmlから変更したクラスです。

[AttributeUsageAttribute(AttributeTargets.Class |
    AttributeTargets.Method, Inherited = true,
    AllowMultiple = true)]
public sealed class BasicAuthorizeAttribute : AuthorizeAttribute
{
    static private string DecodeFrom64(string sEncodedData)
    {
        byte[] encodedDataAsBytes = Convert.FromBase64String(sEncodedData);
        return Encoding.ASCII.GetString(encodedDataAsBytes);
    }

    static private bool GetUserNameAndPassword(HttpActionContext context,
        out string sUserName,
        out string sPassword,
        out bool bCookieAuthorization)
    {
        bCookieAuthorization = false;
        bool bSuccess = false;
        sUserName = sPassword = "";
        IEnumerable<string> headerVals;

        if (context.Request.Headers.TryGetValues("Authorization", out headerVals))
        {
            try
            {
                string sAuthHeader = headerVals.First();
                string[] authHeaderTokens = sAuthHeader.Split();

                if (authHeaderTokens[0].Contains("Basic"))
                {
                    string sDecoded = DecodeFrom64(authHeaderTokens[1]);
                    string[] aPairMembers = sDecoded.Split(new[] { ':' });
                    sUserName = aPairMembers[0];
                    sPassword = aPairMembers[1];
                } 
                else
                {
                    if (authHeaderTokens.Length > 1)
                        sUserName = DecodeFrom64(authHeaderTokens[1]);
                    bCookieAuthorization = true;
                } 

                bSuccess = true;
            }
            catch
            {
                bSuccess = false;
            }
        } 

        return bSuccess;
    }

    static private bool Authenticate(HttpActionContext actionContext,
        out string sUserName)
    {
        bool bIsAuthenticated = false;
        string sPassword;
        bool bCookieAuthorization;

        if (GetUserNameAndPassword(actionContext,
            out sUserName, out sPassword, out bCookieAuthorization))
        {
            // if the header tells us we're using Basic auth then log the user in
            if (!bCookieAuthorization)
            {
                if (WebSecurity.Login(sUserName, sPassword, true))
                    bIsAuthenticated = true;
                else
                    WebSecurity.Logout();
            } 
            // else get authentication from web security
            else
            {
                if (WebSecurity.IsAuthenticated) bIsAuthenticated = true;
                sUserName = WebSecurity.CurrentUserName;
            } 
        } 
        else actionContext.Response =
            new HttpResponseMessage(HttpStatusCode.BadRequest);

        return bIsAuthenticated;
    }

    private bool IsAuthorized(string sUserName)
    {
        SimpleRoleProvider roles =
            (SimpleRoleProvider)System.Web.Security.Roles.Provider;
          string[] aRoles = Roles.Split(new[] {','});

        return (aRoles.Any(sRole => roles.IsUserInRole(sUserName, sRole)));
    }

    public override void OnAuthorization(HttpActionContext actionContext)
    {
        string sUserName;

        if (Authenticate(actionContext, out sUserName))
        {
            if (!IsAuthorized(sUserName))
                actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden);
        } 
        else
        {
            actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
        } 
    }
}

私のクライアント コードは、次のような Javascript (jQuery を使用) を含む単純な HTML ページです。

...
<form>
    <fieldset>
        <legend></legend>
        <ol>
            <li>
                input text
                <input type="text" id="input"/>
            </li>
            <li>
                username
                <input type="text" id="user"/>
            </li>
            <li>
                password
                <input type="password" id="password"/>
            </li>
            <li><a href="#" id="apip">API: JSONP</a></li>
        </ol>
    </fieldset>
</form>
<div id="result"></div>
<script>
    function getAuthorizationHeader(username, password) {
        "use strict";
        var authType;

        if (password == "") {
            authType = "Cookie " + $.base64.encode(username);
        }
        else {
            var up = $.base64.encode(username + ":" + password);
            authType = "Basic " + up;
        };
        return authType;
    };

    function ajaxSuccessHandler(data) {
        "use strict";
        $("#result").text(data);
    };

    function ajaxErrHandler(jqXHR, textStatus, errorThrown) {
        "use strict";
        $("#result").text(errorThrown + " : " + textStatus);
    }

    $(function () {
        "use strict";

        $("#apip").click(function () {
            "use strict";
            var text = $("#input").val();
            $.ajax({
                url: "https://somesiteurl.com/api/wordapi?Text=" + encodeURIComponent(text),
                dataType: "jsonp",
                type: "GET",
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Authorization", getAuthorizationHeader($("#user").val(), $("#password").val()));
                },
                success: ajaxSuccessHandler,
                error: ajaxErrHandler
            });
        });
    });
</script>

CORS

応答が遅くなって申し訳ありません...提案どおりCORSを試していますが、クライアントから送信されたヘッダーにOriginが含まれていないため、明らかに何かが欠けています。http://brockallen.com/2012/06/28/cors-support-in-webapi-mvc-and-iis-with-thinktecture-identitymodel/のライブラリを使用して、私が行ったことは次のとおりです。

  1. このシナリオをテストするために新しい MVC4 インターネット アプリケーションを作成し、NuGet を使用して Thinktecture.IdentityModel を追加します。

  2. App_Start で、この CorsConfig クラスを作成します。

static public class CorsConfig
{
    public static void RegisterCorsForWebApi(HttpConfiguration httpConfig)
    {
        WebApiCorsConfiguration corsConfig = new WebApiCorsConfiguration();

    // this adds the CorsMessageHandler to the HttpConfiguration’s 
    // MessageHandlers collection
    corsConfig.RegisterGlobal(httpConfig);

    corsConfig
        .ForResources("Products")
        .ForOrigins("http://hello.net")
        .AllowAll();
}

public static void RegisterCorsForMvc(MvcCorsConfiguration corsConfig)
{
    corsConfig
        .ForResources("Products.GetProducts")
        .ForOrigins("http://hello.net")
        .AllowAll();
}

}

  1. Global.asax.cs では、このクラスの両方のメソッドを呼び出します。

  2. web.config に次を追加します。

  3. JSON を返す MVC コントローラーで単純なアクション メソッドを作成します。呼び出しがクライアント側で正しく行われたら、後で [Authorize] で装飾する予定です。これにより、認証 (および承認、ロールの追加) をテストできます。

  4. ビューでは、次のようにメソッドを呼び出します。

var text = $("#input").val();
var json = "{'text': " + JSON.stringify(text) + "}";
$.ajax({
    url: "/Home/GetSomeJson",
    dataType: "json",
    data: json,
    type: "GET",
    beforeSend: function (xhr) {
        xhr.withCredentials = true;
    },
    crossDomain: true,
    username: $("#user").val(),
    password: $("#password").val(),
    success: ajaxSuccessHandler,
    error: ajaxErrHandler
});
それでも、ヘッダーを調べると、起源がわかりません。また、これは MVC アクション/WebApi への CORS 呼び出しに対して資格情報を渡す正しい方法ですか (もちろん、実際にはこれは HTTPS になります)。

4

1 に答える 1

2

JSONPを使用する代わりにCORSでWebAPIサービスを有効にしないのはなぜですか?。これは、WebAPIでCORSサポートを有効にする方法を説明するすばらしい投稿です。

于 2013-03-10T20:00:41.953 に答える