Web API中的MVC-6与MVC-5 BearerAuthentication

我有一个Web API项目,使用UseJwtBearerAuthentication到我的身份服务器。 启动时的配置方法如下所示:

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

这是有效的,我想在MVC5项目中做同样的事情。 我试着这样做:

Web api:

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

我不能调用web api如果设置属性[授权](如果我删除它比它工作)

我创建了Startup。 永远不会调用此代码,我不知道要改变什么以使其工作。

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

目标是将来自web api的声明返回给客户端应用程序。 使用承载认证。
感谢帮助。

TL; DR:你不能。

Authority是指在ASP.NET 5中添加到承载中间件的OpenID Connectfunction:在OWIN / Katana版本中没有这样的东西。

注意:Katana有一个app.UseJwtBearerAuthentication扩展,但与其等效的ASP.NET 5不同,它不使用任何OpenID Connectfunction,必须手动配置:您必须提供颁发者名称和用于validation的证书令牌签名: https : //github.com/jchannon/katanaproject/blob/master/src/Microsoft.Owin.Security.Jwt/JwtBearerAuthenticationExtensions.cs

您可以获得索赔:

  IAuthenticationManager AuthenticationManager { get { return Request.GetOwinContext().Authentication; } } public IHttpActionResult UserRoles() { return ok(AuthenticationManager.User.Claims.ToList()); } 

此代码应位于[授权]控制器中。