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()); }
此代码应位于[授权]控制器中。
- 发布错误:无法加载文件或程序集“Microsoft.Web.XmlTransform”,Version = 1.4.0.0,Culture = neutral等等或其依赖项之一
- 跨多个子域的URL路由
- 如何禁用net core 2.1中的预编译视图进行调试?
- 将ASP.Net Core 2(.NET Framework)Web Api拆分为类库和托管项目是否可行/明智?
- 在ASP.NET Core 2.0 Web Api中返回“raw”json
- 用于Mongodb数据存储的asp.net核心中基于令牌的简单身份validation/授权
- ASP.NET MVC 6:在单独的程序集中查看组件
- 在mvc中使用IViewLocationExpander
- ASP.NET 5中System.Web.Mvc.Html.InputExtensions的等价物是什么?
- ASP.NET Core MVC,从数据库获取文件,并呈现为图像
- 基于Http Header值的ASP.NET MVC5 / 6路由