14

Tôi có một dự án API Web sử dụng UseJwtBearerAuthentication cho máy chủ định danh của tôi. Phương thức cấu hình khi khởi động có dạng như sau:MVC-6 vs MVC-5 BearerAuthentication trong API Web

public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
{ 
    app.UseJwtBearerAuthentication(options => 
    { 
     options.AutomaticAuthentication = true; 
     options.Authority = "http://localhost:54540/"; 
     options.Audience = "http://localhost:54540/"; 
    }); 

    // Configure the HTTP request pipeline. 
    app.UseStaticFiles(); 

    // Add MVC to the request pipeline. 
    app.UseMvc(); 
} 

Điều này đang hoạt động và tôi muốn làm điều tương tự trong dự án MVC5. Tôi cố gắng để làm một cái gì đó như thế này:

api web:

public class SecuredController : ApiController 
    { 
      [HttpGet] 
      [Authorize] 
      public IEnumerable<Tuple<string, string>> Get() 
      { 
       var claimsList = new List<Tuple<string, string>>(); 
       var identity = (ClaimsIdentity)User.Identity; 
       foreach (var claim in identity.Claims) 
       { 
        claimsList.Add(new Tuple<string, string>(claim.Type, claim.Value)); 
       } 
       claimsList.Add(new Tuple<string, string>("aaa", "bbb")); 

       return claimsList; 
      } 
} 

tôi không thể gọi api web nếu là thiết lập thuộc tính [ủy quyền] (Nếu tôi loại bỏ điều này hơn là nó đang làm việc)

Tôi đã tạo Startup. Mã này không bao giờ được gọi và tôi không biết phải thay đổi gì để làm cho nó hoạt động.

[assembly: OwinStartup(typeof(ProAuth.Mvc5WebApi.Startup))] 
namespace ProAuth.Mvc5WebApi 
{ 
    public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      ConfigureOAuth(app); 
      HttpConfiguration config = new HttpConfiguration(); 
      WebApiConfig.Register(config); 
      app.UseWebApi(config); 
     } 

     public void ConfigureOAuth(IAppBuilder app) 
     { 
      Uri uri= new Uri("http://localhost:54540/"); 
      PathString path= PathString.FromUriComponent(uri); 

      OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() 
      { 
       AllowInsecureHttp = true, 
       TokenEndpointPath = path, 
       AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), 
       Provider = new SimpleAuthorizationServerProvider() 
      }; 

      // Token Generation 
      app.UseOAuthAuthorizationServer(OAuthServerOptions); 
      app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); 

     } 

    } 

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

     public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
     { 

      context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); 

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

      context.Validated(identity); 

     } 
    } 

} 

mục tiêu là trả lại xác nhận quyền sở hữu từ api web cho ứng dụng khách. bằng cách sử dụng xác thực Bearer.
Cảm ơn bạn đã trợ giúp.

+0

Tôi tìm ứng dụng ở đâu.UseJwtBearerAuthentication (...)? Project.json của bạn và sử dụng các tham chiếu (trên Startup.cs) trông như thế nào? –

+0

"Microsoft.AspNet.Authentication.JwtBearer": "1.0.0- *" – Raskolnikov

+0

Trên mã Khởi động của bạn không được gọi, bạn có thể cần một tập hợp các phụ thuộc nuget phù hợp với MVC5. Tôi chỉ sử dụng phương pháp Global.asax.cs 'Application_Start' thay vì' OwinStartup' – user326608

Trả lời

7

TL; DR: bạn không thể.

Authority đề cập đến một tính năng OpenID Connect đã được thêm vào phần tử trung gian mang trong ASP.NET 5: không có điều gì trong phiên bản OWIN/Katana.

Lưu ý: có phần mở rộng là app.UseJwtBearerAuthentication cho Katana, nhưng không giống với ASP.NET 5, nó không sử dụng bất kỳ tính năng OpenID Connect nào và phải được định cấu hình theo cách thủ công: bạn phải cung cấp tên tổ chức phát hành và chứng chỉ được sử dụng để xác minh chữ ký tokens': https://github.com/jchannon/katanaproject/blob/master/src/Microsoft.Owin.Security.Jwt/JwtBearerAuthenticationExtensions.cs

0

Bạn có thể nhận được tuyên bố như sau:

IAuthenticationManager AuthenticationManager 
{ 
    get 
    { 
     return Request.GetOwinContext().Authentication; 
    } 
} 

public IHttpActionResult UserRoles() 
{ 
return ok(AuthenticationManager.User.Claims.ToList()); 
} 

mã này phải ở trong [Duyệt] điều khiển.