小编sid*_*ker的帖子

为Web Api 2和OWIN令牌身份验证启用CORS

我有一个ASP.NET MVC 5 webproject(localhost:81),它使用Knockoutjs从我的WebApi 2项目(localhost:82)调用函数,以便在我启用CORS的两个项目之间进行通信.到目前为止,一切都有效,直到我尝试对WebApi实施OWIN令牌认证.

要在WebApi上使用/ token端点,我还需要在端点上启用CORS,但经过几个小时的尝试和搜索解决方案后,它仍在运行,并且api/token仍然会导致:

XMLHttpRequest cannot load http://localhost:82/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. 

public void Configuration(IAppBuilder app)
{
    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    TokenConfig.ConfigureOAuth(app);
    ...
}
Run Code Online (Sandbox Code Playgroud)

TokenConfig

public static void ConfigureOAuth(IAppBuilder app)
{
    app.CreatePerOwinContext(ApplicationDbContext.Create);
    app.CreatePerOwinContext<AppUserManager>(AppUserManager.Create);

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

    app.UseOAuthAuthorizationServer(OAuthServerOptions);
    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
Run Code Online (Sandbox Code Playgroud)

AuthorizationProvider

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
    context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

    var appUserManager = context.OwinContext.GetUserManager<AppUserManager>(); …
Run Code Online (Sandbox Code Playgroud)

cors asp.net-web-api owin asp.net-mvc-5 asp.net-web-api2

27
推荐指数
2
解决办法
2万
查看次数

获取API - 重定向有哪些用途:手动

我最近一直在玩Javascript Fetch API.据我了解,默认情况下,所有重定向都是透明处理的,最后我得到了重定向链中最后一次调用的响应.

但是,我可以使用{redirect:'manual'}调用fetch,在这种情况下,它会返回一个没有可用信息的opaqueredirect响应.来自https://fetch.spec.whatwg.org/#concept-filtered-response-opaque-redirect

opaque-redirect过滤响应是一个过滤后的响应,其类型为"opaqueredirect",状态为0,状态消息为空字节序列,标头列表为空,body为空,预告片为空.

https://fetch.spec.whatwg.org/#http-fetch表示如果重定向设置为'manual',则响应将变为opaqueredirect:

切换请求的重定向模式:
...
- manual
    设置对opaque-redirect过滤响应的响应,其内部响应为actualResponse.

规范还说:

换句话说,不透明的过滤响应和不透明重定向过滤的响应几乎与网络错误无法区分.

鉴于这一切,为什么在使用Fetch API时会将重定向设置为手动?对我来说似乎没用.是否存在有用的用例?

javascript specifications fetch-api

26
推荐指数
1
解决办法
1万
查看次数

Spring Boot CORS过滤器 - CORS预检频道没有成功

我需要在我的Spring Boot Web应用程序中添加CORS过滤器.

我已经添加了CORS映射,如以下文档中所述http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html

这是我的配置:

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // @formatter:off   
        registry
            .addMapping("/**")
            .allowedOrigins(CrossOrigin.DEFAULT_ORIGINS)
            .allowedHeaders(CrossOrigin.DEFAULT_ALLOWED_HEADERS)
            .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
            .maxAge(3600L);
        // @formatter:on
    }

...

}
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试访问我的API时,我收到以下错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://example.com/api/v1.0/user. (Reason: CORS preflight channel did not succeed).
Run Code Online (Sandbox Code Playgroud)

这是来自FF控制台的屏幕截图:

在此输入图像描述

我做错了什么以及如何正确配置CORS标头以避免此问题?

spring spring-mvc spring-security cors spring-boot

25
推荐指数
4
解决办法
4万
查看次数

Cloudfront CORS问题在Rails应用程序上提供字体

访问我的网站时,我一直收到来自控制台的错误消息:

font from origin 'https://xxx.cloudfront.net' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.example.com' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)

我尝试了一切:

但没有,零,虚无...

我在Heroku上使用Rails 4.1.

fonts ruby-on-rails heroku cors amazon-cloudfront

23
推荐指数
4
解决办法
6521
查看次数

在Cloudformation模板中为API网关启用CORS

我正在为我的环境创建AWS Cloudformation模板,但我找不到为API Gateway方法启用CORS的方法.

我可以使用AWS控制台配置它(这是官方文档),但是如何在Cloudformation模板中执行此操作?

amazon-web-services cors aws-cloudformation aws-api-gateway

23
推荐指数
1
解决办法
1万
查看次数

处理响应 - SyntaxError:使用模式时意外的输入结束:'no-cors'

我尝试了对REST-API的ReactJS fetch调用,并希望处理响应.通话有效,我收到回复,我可以在Chrome开发工具中看到:

function getAllCourses() {
fetch('http://localhost:8080/course', {
    method: 'POST',
    mode: 'no-cors',
    credentials: 'same-origin',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        objectClass: 'course',
        crud: '2'
    })
}).then(function (response) {
    console.log(response);
    return response.json();

}).catch(function (err) {
    console.log(err)
});
}
Run Code Online (Sandbox Code Playgroud)

当我尝试处理响应时,我得到了一个"SyntaxError:意外的输入结束"

return response.json();
Run Code Online (Sandbox Code Playgroud)

console.log看起来像这样:

CONSOLE.LOG(响应)

我的响应JSON看起来像这样,它是有效的,我用jsonlint检查它:

[
  {
    "0x1": {
      "users": [],
      "lectures": [],
      "owner": "0x2",
      "title": "WWI 14 SEA",
      "description": null,
      "objectClass": "course",
      "id": "course_00001"
    },
    "0x2": {
      "username": "system",
      "lectures": [],
      "course": null,
      "solutions": [],
      "exercises": [],
      "roles": [
        "0x3", …
Run Code Online (Sandbox Code Playgroud)

javascript json cors reactjs fetch-api

22
推荐指数
1
解决办法
3万
查看次数

Sec-Fetch-Mode 和阻塞的 CORS

所以我有同一个网站,从不同的网络和计算机向 (1) Chrome 76 和 (2) Chrome 77 上的同一台服务器发出相同的请求。

一个请求有 (1)Sec-Fetch-Mode: no-cors, Sec-Fetch-Site: cross-site和另一个 (2) Sec-Fetch-Mode: cors, Sec-Fetch-Site: same-site。与一个no-cors具有400与CORS一个C#的Web API端点启用(多年,数以千计的各类设备的不同用户的)失败。

到底是怎么回事?有传言说 Chrome错误没有发送该标头进行飞行前检查,但它确实存在并设置为no-cors.

Chrome 中的安全设置或错误?可修复的服务器端还是前端?

这是由 XMLHttpRequest 发送的,而不是新的 Fetch-API。

google-chrome cors http-status-code-400 sec-fetch-site sec-fetch-mode

22
推荐指数
0
解决办法
1万
查看次数

Push API和Server Sent Events之间有什么区别?

从文档中我可以看出Push APIServer Sent Events 都是半双工但为什么两种不同的技术具有相同的功能?Push API中有更重要的东西吗?

html5 push-notification server-sent-events web-push push-api

21
推荐指数
2
解决办法
6728
查看次数

在Android的谷歌浏览器中,如何设置unsafely-treat-insecure-origin-as-secure

getUserMedia()在我的网络应用程序中使用,当我在localhost上测试我的应用程序时工作正常.但是,如果我将我的笔记本电脑视为服务器并在我的Android手机的谷歌Chrome浏览器中启动应用程序,它会给我错误:

getUserMedia()不再适用于不安全的起源.要使用此功能,您应该考虑将应用程序切换到安全的来源,例如HTTPS.有关详细信息,请参阅https://goo.gl/rStTGz.

当我检查[ https://goo.gl/rStTGz] [1 ]时,我知道getUserMedia()在不安全的起源上已弃用.写的是对于开发模式,

您可以使用--unsafely-treat-insecure-origin-as-secure ="example.com"标志运行chrome(将"example.com"替换为您实际要测试的原点)

如何以及在哪里设置此标志?还有其他选择吗?

android google-chrome getusermedia

21
推荐指数
2
解决办法
5666
查看次数

预检的响应在角度中没有HTTP ok状态

我在我的服务中定义了以下get请求:

createAuthorizationHeader(user, pass) {
  let headers: HttpHeaders = new HttpHeaders;
  headers = headers.append('Accept', 'application/json, text/plain, */*');
  headers = headers.append('Authorization', 'Basic ' + btoa(user + ':' + pass));
  headers = headers.append('Content-Type', 'application/json; charset=utf-8');
    console.log(headers);
    return this.http.get(this._loginUrl, {
      headers: headers
    });
}
Run Code Online (Sandbox Code Playgroud)

结果是:

OPTIONS https://localhost:8443/delivery/all 401 ()

Failed to load https://localhost:8443/delivery/all: Response for preflight does not have HTTP ok status.

HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …}
Run Code Online (Sandbox Code Playgroud)

我也和Postman做了相同的帖子请求,但一切正常,所以本地服务器工作.

我无法弄清楚我的要求是怎么回事.

http cors angular

21
推荐指数
4
解决办法
4万
查看次数