2015-05-13 26 views
11

Tôi đã xuất phát OAuthAuthorizationServerProvider để xác thực cả khách hàng và chủ sở hữu tài nguyên.Tùy chỉnh mã trạng thái HTTP OWIN/OAuth khi từ chối yêu cầu mã thông báo

Khi tôi xác nhận chủ sở hữu tài nguyên tôi thấy thông tin của họ là không hợp lệ, tôi gọi context.Rejected(), và HTTP response đi kèm với HTTP/400 Bad Request mã trạng thái trong khi tôi mong chờ HTTP/401 Unauthorized.

Tôi làm cách nào để tùy chỉnh mã trạng thái HTTP phản hồi của OAuthAuthorizationServerProvider?

Trả lời

12

Đây là cách chúng tôi ghi đè lên OwinMiddleware ... đầu tiên chúng tôi tạo phần mềm trung gian của riêng mình trên đầu trang của Owin ... Tôi nghĩ chúng tôi có vấn đề tương tự như bạn đã làm.

Trước tiên cần phải tạo ra một hằng số:

public class Constants 
{ 
    public const string OwinChallengeFlag = "X-Challenge"; 
} 

Và chúng tôi ghi đè OwinMiddleware

public class AuthenticationMiddleware : OwinMiddleware 
{ 
    public AuthenticationMiddleware(OwinMiddleware next) : base(next) { } 

    public override async Task Invoke(IOwinContext context) 
    { 
     await Next.Invoke(context); 

     if (context.Response.StatusCode == 400 && context.Response.Headers.ContainsKey(Constants.OwinChallengeFlag)) 
     { 
      var headerValues = context.Response.Headers.GetValues(Constants.OwinChallengeFlag); 
      context.Response.StatusCode = Convert.ToInt16(headerValues.FirstOrDefault()); 
      context.Response.Headers.Remove(Constants.OwinChallengeFlag); 
     } 

    } 
} 

Trong tập tin startup.Auth, chúng tôi cho phép overrid của Invoke Owin Commands

public void ConfigureAuth(IAppBuilder app) 
    .... 
     app.Use<AuthenticationMiddleware>(); //Allows override of Invoke OWIN commands 
    .... 

    } 

Và trong ApplicationOAuthProvider, chúng tôi đã sửa đổi GrantResourceOwnerCredentials.

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
    { 
     using (UserManager<IdentityUser> userManager = _userManagerFactory()) 
     { 
      IdentityUser user = await userManager.FindAsync(context.UserName, context.Password); 

      if (user == null) 
      { 
       context.SetError("invalid_grant", "The user name or password is incorrect."); 
       context.Response.Headers.Add(Constants.OwinChallengeFlag, new[] { ((int)HttpStatusCode.Unauthorized).ToString() }); //Little trick to get this to throw 401, refer to AuthenticationMiddleware for more 
       //return; 
      } 
      .... 
+0

Tôi sẽ cố gắng vào ngày mai và tôi sẽ xác nhận bạn có làm việc cho tôi không !! Cảm ơn bạn trước;) –

+0

Hey, tôi đã thử cách tiếp cận của bạn và tôi đã tìm kiếm vấn đề. Tôi đoán 'STTIAuthenticationMiddleware' là' AuthenticationMiddleware', và vấn đề chính của tôi là middleware tùy chỉnh không bao giờ được gọi trong suốt đường ống của tôi. Tôi đã cấu hình phần mềm trung gian của tôi bằng cách sử dụng 'app.Use ()' và 'app.Use (System.Type)'. Chuyện gì vậy? –

+0

Có, ở phần đầu, chúng giống nhau. Mặt khác, bạn đã xác minh rằng startup.auth đang gọi ConfigureAuth và thiết lập nó chưa? –

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