2015-05-20 9 views
6

काम नहीं कर रहा मैं एक Auth-प्रदाता, जो दावे-पहचान सेटOAuth वाहक टोकन

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); 
    } 
} 

मैं हैलो-दुनिया-api जिसका उपयोग अनधिकृत पहुंच त्रुटि दे रहा है का उपयोग करने की कोशिश कर रहा हूँ का एक न्यूनतम सेटअप।

public class HelloWorldApiController : ApiController 
{ 

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

लेकिन मुझे उपर्युक्त एपीआई के लिए 401/अनधिकृत पहुंच मिल रही है। मुझे वेब-एपीआई में भालू टोकन वापस मिल गया है और मैं इसे सर्वर पर Bearer ABCD**** के रूप में पास कर रहा हूं। मैं देखता हूं कि विजुअल स्टूडियो में डिबगिंग करते समय प्राधिकरण हेडर सेट किया गया है।

अगर मैं AuthorizeAttribute डिबग, मैं user.Identity.IsAuthenticatedfalse, जो वास्तव में समस्या पैदा कर रहा है के रूप में हो रही है। लेकिन यह देखते हुए कि मुझे प्राधिकरण हेडर सेट दिखाई देता है और मैंने OAuthProvider में दावा विवरण सेट किए हैं, तो यह क्यों है कि AuthorizeAttribute उस जानकारी को पढ़ नहीं रहा है?

नोट: यह एक वेब एपीआई प्रोजेक्ट है इसलिए एमवीसी AuthorizeAttribute के लिए कोई संदर्भ नहीं हैं। नहीं करने के लिए

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()); 
    } 
} 

उत्तर

4

यह काम config.Filters.Add(new HostAuthenticationAttribute("bearer")); अधिकृत विशेषता beofre इस लाइन को जोड़ने के लिए आवश्यक बनाने के लिए ...

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' को किसी भी असेंबली संदर्भ की आवश्यकता है? क्या मुझे 'config.EnableCors (...)' का उपयोग करने के लिए एक्सटेंशन विधि की आवश्यकता है? – Bellash

+1

आपको nugget इंस्टॉल करने की आवश्यकता है:। Microsoft.owin.cors – harishr

1

एक अन्य संभावित समाधान जो मेरे लिए काम किया था:

यहाँ Owin सेटअप है HostAuthenticationAttribute का उपयोग करें, लेकिन इसके बजाय ओविन फ़िल्टर को इस तरह एक सक्रिय फ़िल्टर होने के लिए सेट करें:

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

भालू ऑप्शन वैरिएबल को क्या सौंपा गया है? धन्यवाद –

संबंधित मुद्दे