asp.net-mvc – CORS在使用OWIN认证的web api中不起作用
发布时间:2020-12-15 19:26:21 所属栏目:asp.Net 来源:网络整理
导读:在我的应用程序中,我正在使用基于令牌的身份验证和基于CORS支持的web api,但是当客户端请求令牌时,由于CORS(跨原始请求被阻止:同源原则策略)不允许读取远程资源(我的站点)名称),这可以通过将资源移动到同一个域或启用CORS来修复.) 我已经配置了CORS支持所需
在我的应用程序中,我正在使用基于令牌的身份验证和基于CORS支持的web api,但是当客户端请求令牌时,由于CORS(跨原始请求被阻止:同源原则策略)不允许读取远程资源(我的站点)名称),这可以通过将资源移动到同一个域或启用CORS来修复.)
我已经配置了CORS支持所需的一切(我认为这样).这里我的配置 欧文启动班 public class Startup { public void Configuration(IAppBuilder app) { var config = new HttpConfiguration { DependencyResolver = new StructureMapWebApiDependencyResolver(container) }; WebApiConfig.Register(config); // registering web api configuration app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); // cors for owin token pipeline app.UseWebApi(config); ConfigureOAuth(app); } public void ConfigureOAuth(IAppBuilder app) { var oAuthAuthorizationServerOptions = new OAuthAuthorizationServerOptions() { AllowInsecureHttp = true,TokenEndpointPath = new PathString("/token"),AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),Provider = new SimpleAuthorizationServerProvider() }; // Token Generation app.USEOAuthAuthorizationServer(oAuthAuthorizationServerOptions); app.USEOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); } } 和我的webapi配置 public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.EnableCors(); // Corse support for Web api config.MapHttpAttributeRoutes(); // attribute based urls config.Routes.MapHttpRoute( name: "DefaultApi",routeTemplate: "api/{controller}/{id}",defaults: new { id = RouteParameter.Optional } ); } } 这里配置在web.config <system.webserver> <httpProtocol> <customHeaders> <!-- Adding the following custom HttpHeader will help prevent CORS from stopping the Request--> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Methods" value="GET,POST,OPTIONS,PUT,DELETE" /> </customHeaders> </httpProtocol> </system.webserver> 和我的请求标题从mozilla Accept application/json,text/plain,*/* Accept-Encoding gzip,deflate Accept-Language en-US,en;q=0.5 Content-Length 67 Content-Type application/x-www-form-urlencoded; charset=UTF-8 Host talenterp Origin http://192.168.1.11:85 Referer http://192.168.1.11:85/ User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0 应用程序的URL是 服务器应用程序(应支持CORS) {http://talenterp} 令牌终点: {http://talenterp/token} 客户端应用 {http://talentmvc:85} 注意:我已经添加 context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin",new[] { "*" }); 在我的AuthorizationServerProvider的GrantResourceOwnerCredentials()方法中 解决方法
确保你只有
配置,而不是您的Global.asax或WebApiConfig中的旧样式“config.EnableCors()”.此外:将上述语句作为第一个语句放在您的owin启动类中.是的,真的有所作为,稍后再设置也可能导致cors不起作用. public partial class Startup { public void Configuration(IAppBuilder app) { app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); ... etc (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-identity – 保护整个ASP.NET 5 MVC 6应用程序
- 跟着老桂学ASP.NET Core 2.0
- asp.net-mvc – 如何在OWIN OAuth .NET中获取UserID
- asp.net – Microsoft MVC“echo / print / output”等
- asp.net-mvc – RouteCollection.Ignore和RouteCollection.
- 如何解决分布式系统中的跨时区问题[原理篇]
- asp.net-mvc-3 – 如何重定向已登录的用户,该用户正在尝试访
- [ASP.NET] 网页导向
- ASP.NET – 从静态方法/静态类访问会话?
- asp.net-mvc-3 – MVC按钮点击动作
推荐文章
站长推荐
- asp.net – 无法在UpdatePanel中下载文件
- 框架升级后某个类型所在程序集发生转移,应用还能
- asp.net-mvc – MVC架构 – 重新使用相同的viewm
- nTier应用程序中的.Net会员资格
- asp.net-core – 如何自动增加MVC 6版本号?
- wcf – 如何使用Fiddler收听asp.net开发服务器(即
- asp.net – System.Web.Providers不适用于中等信
- asp.net-mvc – 如何在MVC4 WebAPI配置中忽略路由
- asp.net-mvc-3 – MVC3将基类传递给局部视图 –
- asp.net-mvc – 为什么我的MVC应用程序中有两个w
热点阅读