1

これを理解するために、asp.net のサンプルアプリケーションを使用しています。実行してサンプルページを使用すると、すべてがうまくいきます。asp.netアプリケーションからの認証、Oauth

次に、mvc scaffoldingを有効にした新しい空のasp.netプロジェクトを作成し、2つの異なるレイヤーでシナリオを再作成します。

<form method="post" action="https://localhost:44305/api/Account/Register"> 
     <h3>Register</h3> 
     <div class="form-group"> 
      <label>Email</label> 
      @Html.TextBoxFor(m => m.Email, new { id = "txtUserName", @class = "form-control" }) 
     </div> 
     <div class="form-group"> 
      <label>Password</label> 
      @Html.PasswordFor(m => m.Password, new { id = "txtPassword", @class = "form-control" }) 
     </div> 
     <div class="form-group"> 
      <label>Confirm Password</label> 
      @Html.PasswordFor(m => m.ConfirmPassword, new { id = "txtConfirmPassword", @class = "form-control" }) 
     </div> 
     <div class="form-group"> 
      <button type="submit" class="btn btn-default">Register</button> 
     </div> 
    </form> 

が、私はそれが私は、クエリ文字列として入力されたユーザ名/パスワードと同じページにリダイレクトログインしようとすると:私は、このフォームアクションを使用して登録することができるよ(http://localhost:57972/?Email=username&Password=password

 <div class="form-group"> 
      <label>Email</label> 
      @Html.TextBoxFor(m => m.Email, new { id = "txtloginUserName", @class = "form-control" }) 
     </div> 
     <div class="form-group"> 
      <label>Password</label> 
      @Html.PasswordFor(m => m.Password, new { id = "txtloginPassword", @class = "form-control" }) 
     </div> 
     <div class="form-group"> 
      <button id="btnLogin" class="btn btn-default">Log In</button> 
     </div> 
<script> 
    var token = ""; 
    $('#btnLogin').click(function() { 
     var username = $('#txtloginUserName').val(); 
     var password = $('#txtloginPassword').val(); 

     $.ajax({ 
      type: 'POST', 
      url: 'http://localhost:26812/Token', 
      contentType: 'application/x-www-form-urlencoded; charset=UTF-8', 
      data: { 
       grant_type: 'password', 
       username: username, 
       password: password 
      }, 
      success: function (data) { 
       token = data.access_token; 
      } 
     }) 
    }); 
</script> 
:私は私がログインして、それが200のOKステータスコードで応答しますが、その後、コンソールにエラーがポップアップするネットワークパケットを見ると今/トークンの完全なURLを呼び出すとしていたが実現

**更新

"いいえ 'Access-Control-Allow-Origin'ヘッダー要求されたリソースに存在します。起源「http://localhost:57972」のため、アクセスが許可されていません「

私は私のwebapiconfigへ)(config.EnableCorsを追加しました、と私はテスト目的のためにすべてを許可するように私のapicontrollerに注釈を追加しました:。

[EnableCors(origins: "*", headers:"*", methods:"*")] 

Picture of the network response and error:

+0

の確認は、事前flighから 'いいえ「アクセス制御 - 許可 - Origin''エラーです「OPTIONS」リクエスト、そうですか? – Searching

+0

奇妙な部分はPOST要求自体がOKを返した後にそのエラーがスローされますが、ログには1つの要求が表示され、POSTです。 –

+0

MVC APIを使った 'Access-Control-Allow-Origin'には数多くの質問があります。あなたがOPTIONSにこれを持っていないのは良いことです。 ajaxリクエストの 'datatype'を' dataType: "jsonp" 'に設定して、まだこの問題があるかどうか確認してください。 – Searching

答えて

0

あなたのGlobal.asaxに、このメソッドを追加してみてください:

protected void Application_BeginRequest(object sender, EventArgs e) 
    { 
     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); 
     if (HttpContext.Current.Request.HttpMethod == "OPTIONS") 
     { 
      HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true"); 
      HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, DELETE, PUT"); 
      HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Authentication"); 
      HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "86400"); 
      HttpContext.Current.Response.End(); 
     } 
     else 
     { 
      HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true"); 
      HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS, DELETE"); 
     } 
    } 
関連する問題