OWIN上的CORS和访问/令牌导致“Access-Control-Allow-Origin”错
发布时间:2020-12-15 22:57:45 所属栏目:asp.Net 来源:网络整理
导读:我有使用owin中间件保护我的Web API有困难. 我已经安装下面的包装 Install-Package Microsoft.Owin.Cors -Version 2.1.0 以下是ConfigureAuth.cs代码. public void ConfigureAuth(IAppBuilder app) { //... app.USEOAuthBearerTokens(OAuthOptions); ///Inst
我有使用owin中间件保护我的Web API有困难.
我已经安装下面的包装 Install-Package Microsoft.Owin.Cors -Version 2.1.0 以下是ConfigureAuth.cs代码. public void ConfigureAuth(IAppBuilder app) { //... app.USEOAuthBearerTokens(OAuthOptions); ///Install-Package Microsoft.Owin.Cors -Version 2.1.0 app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); } 我已经在一个链接上举办了这个WebApi项目,比如http://webaip.azurewebsites.net 我试图从另一个网站访问上述API的控制器方法,比如http://mysite.azurewebsites.net function LogIn() { var loginData = { grant_type: 'password',username: 'username',password: 'password',}; $.ajax({ type: 'POST',url: 'http://webaip.azurewebsites.net/Token/',data: loginData }).done(function (data) { alert('logged in'); alert(data); }).fail(function (data) { alert('login problem') }).error(function (data) { alert('error invoking API'); }); return false; } 我下错了 XMLHttpRequest cannot load http://webaip.azurewebsites.net/Token/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mysite.azurewebsites.net' is therefore not allowed access. The response had HTTP status code 404. 注意:我也尝试使用下面的代码.这也不是为我工作. public static void Register(HttpConfiguration config) { var json = config.Formatters.JsonFormatter; config.Formatters.Remove(config.Formatters.XmlFormatter); //Need to have Microsoft.AspNet.WebApi.Cors package installed. config.EnableCors(new EnableCorsAttribute("*","*","*")); } 解决方法
您收到该错误的原因是因为您已经为webapi启用了CORS,但不是为您的/ Token端点启用,所以在webapi管道获取其CORS设置之前,将初始化它.
所以除了你在WebApiConfig.cs中已经做的事情外, 您应该执行以下操作:(假设您有一个标准的WebAPI 2项目) **打开文件:App_Start / IdenityConfig.cs **并添加以下行//允许cors为… 我已经把它们放在正常的项目模板中了 public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,IOwinContext context) { // Allows cors for the /token endpoint this is different from webapi endpoints. context.Response.Headers.Add("Access-Control-Allow-Origin",new[] { "*" }); // <-- This is the line you need var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<IdentityDb>())); // Configure validation logic for usernames manager.UserValidator = new UserValidator<ApplicationUser>(manager) { AllowOnlyAlphanumericUserNames = true,RequireUniqueEmail = true }; // Configure validation logic for passwords manager.PasswordValidator = new PasswordValidator { RequiredLength = 6,RequireNonLetterOrDigit = false,RequireDigit = true,RequireLowercase = true,RequireUppercase = true,}; // rest ommited ... return manager; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- C#使用Linq to csv读取.csv文件数据
- .net – App_LocalResources如何使用MVC?
- asp.net-mvc – 在“控制器操作”中访问操作过滤器的数据
- asp.net-mvc-3 – 如何判断我的动作是否被RenderAction调用
- 如何在asp.net(C#)中创建动态页面?
- 在asp.net中使用太多会话的缺点
- ASP.NET 2.0 – 带有tbody / thead的DataGrid
- asp.net-mvc – Sitecore搜索方面和计算字段
- asp.net-mvc – Asp.net Mvc 2:存储库,分页和过滤如何?
- asp.net – coldfusion和.net上的单点登录
推荐文章
站长推荐
- ASP.NET MVC 2中是否存在强类型的RedirectToActi
- asp.net-mvc – ASP.NET MVC:与非移动视图一起使
- Tridion页面中的这些ASP.Net用户控件(.ascx)有什
- asp.net-mvc-3 – MVC3强类型的局部视图模型绑定
- asp.net-mvc – Asp.net MVC 3 RC – Razor Vali
- asp.net-mvc – .net MVC控制器动作方法的属性
- asp.net – 在新的MVC4应用程序中获取UserId(int
- entity-framework – ViewModels和MVC中Entity F
- asp.net文件上传示例分享
- asp.net-mvc-3 – 使用asp.net MVC在Orchard CMS
热点阅读