2014-12-08 16 views
7

Tôi đọc một số bài viết nói về một số vấn đề tương tự, nhưng tôi vẫn chưa thực hiện việc này để làm việc.MVC 5 OAuth với CORS

Tôi đang thực hiện ajax cho "Tài khoản/ExternalLogin" để tạo ChallengeResult và bắt đầu luồng xác thực bằng OWIN.

Đây là lớp Startup tôi:

public partial class Startup 
{ 
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864 
    public void ConfigureAuth(IAppBuilder app) 
    {    
     // Enable the application to use a cookie to store information for the signed in user 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      LoginPath = new PathString("/Account/Login") 
     }); 
     // Use a cookie to temporarily store information about a user logging in with a third party login provider 
     app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 

     app.UseCors(CorsOptions.AllowAll); 
     var goath2 = new Microsoft.Owin.Security.Google.GoogleOAuth2AuthenticationOptions 
     { 
      ClientId = "myclientid", 
      ClientSecret = "mysecret", 
      Provider = new Microsoft.Owin.Security.Google.GoogleOAuth2AuthenticationProvider 
      { 
       OnApplyRedirect = context => 
       { 
        string redirect = context.RedirectUri; 

        const string Origin = "Origin"; 
        const string AccessControlAllowOrigin = "Access-Control-Allow-Origin"; 

        // origin is https://localhost:44301       
        var origin = context.Request.Headers.GetValues(Origin).First(); 


        // header is present 
        var headerIsPresent = context.Response.Headers.ContainsKey(AccessControlAllowOrigin); 
        context.Response.Redirect(redirect);       
       } 
      } 
     }; 

     app.UseGoogleAuthentication(goath2); 
    } 
} 

Tôi cho phép CORS hỗ trợ hộ dòng app.UserCors(CorsOptinos.AllowAll); Và tôi biết tiêu đề đang được thêm vào câu trả lời vì tôi chặn sự kiện OnApplyRedirect và khi tôi tìm kiếm nguồn gốc nó được đặt thành 'localhost: 443001' và tiêu đề 'Access-Control-Allow-Origin' cũng được đặt thành giá trị này.

Tuy nhiên khi câu trả lời sẽ được gửi đến khách hàng Tôi có lỗi sau:

XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxx No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin https://localhost:44301 is therefore not allowed access.

Những gì tôi đang mất tích ở đây.

Tôi có thể làm việc xung quanh làm tất cả điều này "theo cách thủ công" (yêu cầu trực tiếp từ máy khách của Google ...) nhưng tôi thực sự muốn sử dụng phần mềm trung gian OWIN.

Trả lời

0

Bạn đang yêu cầu google từ miền https://localhost:44301. Để làm việc 'Access-Control-Allow-Origin' phải có tên miền gốc 'https://localhost:44301' trong danh sách. Vì vậy, trong trường hợp này, Google cần đặt tên miền này trong 'Access-Control-Allow-Origin'.

Nhìn vào phản hồi bạn nhận được dường như Google không cho phép yêu cầu xuất xứ Cros từ miền của bạn. Để giải quyết vấn đề này, tôi tin rằng bạn cần đăng ký miền của mình trên google https://developers.google.com.

Các vấn đề liên quan