2015-05-20 17 views
6

Tôi có một thiết lập tối thiểu của một auth-nhà cung cấp, trong đó đặt ra yêu cầu-sắcOAuth mang dấu hiệu không làm việc

public class SimpleAuthorizationProvider : OAuthAuthorizationServerProvider 
{ 
    public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) 
    { 
     context.Validated(); 
    } 

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
    { 
     if (context.UserName != context.Password) 
     { 
      context.SetError("invalid_grant", "The user name or password is incorrect."); 
      return; 
     } 

     var identity = new ClaimsIdentity(context.Options.AuthenticationType); 
     identity.AddClaim(new Claim("sub", context.UserName)); 
     identity.AddClaim(new Claim("role", "user")); 

     context.Validated(identity); 
    } 
} 

tôi đang cố gắng để truy cập hello-thế giới-api, được đưa ra lỗi truy cập trái phép.

public class HelloWorldApiController : ApiController 
{ 

    [HttpGet] 
    [Route("api/hello")] 
    //[AllowAnonymous] 
    [Authorize] 
    public HttpResponseMessage FetchAllEnum() 
    { 
     return Request.CreateResponse(HttpStatusCode.OK, "Hello World!!!"); 
    } 
} 

Nhưng tôi nhận được 401/truy cập trái phép cho API ở trên. Tôi nhận được mã thông báo mang về trang web-api và tôi cũng chuyển nó tới máy chủ là Bearer ABCD****. Tôi thấy rằng tiêu đề ủy quyền được đặt trong khi gỡ lỗi trong Visual Studio.

Nếu tôi gỡ lỗi AuthorizeAttribute, tôi nhận được user.Identity.IsAuthenticatedfalse, điều này thực sự gây ra sự cố. Nhưng với điều kiện tôi thấy bộ tiêu đề Ủy quyền và tôi đã đặt chi tiết xác nhận quyền sở hữu theo số OAuthProvider, tại sao số AuthorizeAttribute không đọc thông tin đó?

Lưu ý: Đây là dự án API Web nên không có tham chiếu đến thuộc tính MVC AuthorizeAttribute.

Đây là thiết lập OWIN:

public static class WebApiConfig 
{ 
    public static HttpConfiguration Register() 
    { 
     var config = new HttpConfiguration(); 
     config.MapHttpAttributeRoutes(); 
     //config.SuppressDefaultHostAuthentication(); //tried with/without this line 
     config.Filters.Add(new AuthorizeAttribute()); 
     config.EnableCors(new EnableCorsAttribute("*", "*", "*", "*")); 
     return config; 
    } 
} 

public class OwinConfiguration 
{ 
    // ReSharper disable once UnusedMember.Local 
    public void Configuration(IAppBuilder app) 
    { 
     ConfigureOAuth(app); 
     app.UseCors(CorsOptions.AllowAll); 
     app.UseWebApi(WebApiConfig.Register()); 
    } 

    private void ConfigureOAuth(IAppBuilder app) 
    { 
     var options = new OAuthAuthorizationServerOptions 
     { 
      AllowInsecureHttp = true, 
      TokenEndpointPath = new PathString("/token"), 
      AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(60), 
      Provider = new SimpleAuthorizationProvider() 
     }; 

     app.UseOAuthAuthorizationServer(options); 
     app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); 
    } 
} 

Trả lời

4

để làm cho nó hoạt config.Filters.Add(new HostAuthenticationAttribute("bearer")); cần thiết để thêm dòng này beofre thuộc tính ủy quyền ...

public static HttpConfiguration Register() 
{ 
    var config = new HttpConfiguration(); 
    config.MapHttpAttributeRoutes(); 

    config.Filters.Add(new HostAuthenticationAttribute("bearer")); //added this 
    config.Filters.Add(new AuthorizeAttribute()); 
    config.EnableCors(new EnableCorsAttribute("*", "*", "*", "*")); 
    return config; 
} 
+0

'EnableCorsAttribute' có yêu cầu tham chiếu assembly không? Tôi có cần phương thức mở rộng để sử dụng 'config.EnableCors (...)'? – Bellash

+1

Bạn cần cài đặt nugget:. Microsoft.owin.cors – harishr

1

Một giải pháp khả thi mà làm việc cho tôi là không sử dụng HostAuthenticationAttribute, nhưng thay vì đặt bộ lọc OWIN thành bộ lọc Hoạt động như sau:

var bearerOptions = new OAuthBearerAuthenticationOptions 
{ 
    AccessTokenFormat = new JwtFormat(validationParameters), 
    AuthenticationMode = AuthenticationMode.Active, 
}; 
+0

biến bearerOptions được gán cho điều gì? Cảm ơn –

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