相关疑难解决方法(0)

Web Api 2预检CORS对承载令牌的请求

我有一个带有AngularJS前端和Web Api 2后端的Web应用程序,它使用bearer-tokens进行身份验证.

一切都在FireFox和IE中很好,但对于Chrome,我的初始登录请求是SOMETIMES预先通过.

这是来自AngularJS服务的调用:

$ http.post(http://localhost:55483/token,data,{headers:{'Content-Type':'application/x-www-form-urlencoded'}}).success(function(response){...});

预检请求因"Allow-Access-Control-Origin"错误而被踢回.

但是,如果我再次单击"登录"按钮(从而重新发送上述请求),一切都很顺利.

关于如何防止/陷阱/处理这个的任何想法?

PS:我使用LOC

context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
Run Code Online (Sandbox Code Playgroud)

ApplicationOAuthProvider.cs文件中将CORS allow-header放在/ Token请求上,这在IE,FireFox和Chrome中都可以正常工作.

cors angularjs asp.net-web-api2

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

405 WebAPI2上不允许的方法(OWIN)

我正在为我的API设置一个WebAPI端点,但是我无法使用我的PostRegister方法调用AngularJS.

webAPI和Angular位于不同的站点上.

在Startup.cs我有:

    public void Configuration(IAppBuilder app)
    {
        ConfigureOAuth(app);

        var config = new HttpConfiguration();

        WebApiConfig.Register(config);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseWebApi(config);

    }

    private void ConfigureOAuth(IAppBuilder app)
    {
        var oAuthServerOptions = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString(Paths.TokenPath),
            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
            Provider = Kernel.Get<IOAuthAuthorizationServerProvider>()
        };
        app.UseOAuthAuthorizationServer(oAuthServerOptions); 

        OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
        app.UseOAuthBearerAuthentication(OAuthBearerOptions);
    }
Run Code Online (Sandbox Code Playgroud)

然后在控制器上,我的方法被设置为允许匿名:

    [AllowAnonymous]
    public bool PostRegister(UserSignupForm form)
    {
        ...
    }
Run Code Online (Sandbox Code Playgroud)

我还更新了web.config:

 <system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer> …
Run Code Online (Sandbox Code Playgroud)

cors asp.net-web-api angularjs owin asp.net-web-api2

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

CORS错误 - 请求的资源上没有"Access-Control-Allow-Origin"标头

我使用Angular前端连接到WEB API 2后端.失败的用例如下.当用户注册成功注册时,他们必须登录系统并重定向到新页面以收集更多信息.我正在使用TOKENS进行身份验证.

我在WebAPI配置中启用了CORS:

 var cors = new EnableCorsAttribute("http://localhost:7812", "*", "*");
        config.EnableCors(cors);   
Run Code Online (Sandbox Code Playgroud)

注册请求成功,响应头具有所需的CORS头:

**Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:7812**
Content-Length:0
Date:Sun, 24 Aug 2014 09:31:55 GMT
Server:Microsoft-IIS/8.0
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?QzpcUHJvamVjdHNcVGVzdGluZ1xNYWx0QXBhcnRtZW50c1xNYWx0YXBhcnRtZW50cy5BUElcTWFsdGFwYXJ0bWVudHMuQVBJXGFwaVxhY2NvdW50XHJlZ2lzdGVy?=
Run Code Online (Sandbox Code Playgroud)

在下一步中,我尝试将用户登录到系统.作为登录的一部分,前端从服务器请求TOKEN Request URL:http://localhost:7802/token.请求标头再次发送一个Origin标头,Origin:http://localhost:7812但这次我收到错误:XMLHttpRequest cannot load http://localhost:7802/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:7812' is therefore not allowed access.

有人有主意吗?

asp.net cors asp.net-web-api angularjs asp.net-web-api2

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

Angular - > Web Api 2:"没有'Access-Control-Allow-Origin'标题"

基本上我正在尝试访问上传到Azure上的web api.

我已将我的CORS属性添加到我的WebApiConfig类:

public static void Register(HttpConfiguration config)
{
    var cors = new EnableCorsAttribute("http://localhost:51407", "*", "*");
    config.EnableCors(cors);
Run Code Online (Sandbox Code Playgroud)

但是每当我发送一个正确包含的Ajax请求(使用angularJS)时

Origin: http://localhost:51407
Run Code Online (Sandbox Code Playgroud)

我收到错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:51407' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)

可能是什么问题?

api azure cors asp.net-web-api angularjs

7
推荐指数
1
解决办法
7539
查看次数

CORS预检请求在Azure托管的Web API中响应302重定向

场景:

我有两个在Windows Azure上单独托管的ASP.NET Web应用程序,它们都与同一个Azure Active Directory租户相关联:

  1. 带有AngularJS SPA前端的MVC应用程序和用于在客户端上处理Azure AD身份验证的adal.js库.

  2. 带有Microsoft OWIN中间件的Web API,用于在服务器上处理Azure AD身份验证.

问题:

当角度引导客户端应用程序时,页面在通过oauth重定向到正确的Identity Authority后正确加载,并且adal.js库正确检索并存储每个应用程序的不同令牌(通过检查"资源/会话 - 存储"选项卡进行验证Chrome开发工具).但是当客户端应用程序尝试访问或更新API中的任何数据时,CORS预检请求正在响应302重定向到Identity Authority,这会导致控制台中出现以下错误:

XMLHttpRequest无法加载https://webapi.azurewebsites.net/api/items.请求被重定向到' https://login.windows.net/ {authority-guid}/oauth2/authorize?response_type = id_token&redirect_uri = .... etc..etc ..',这对于跨源请求是不允许的这需要预检.

示例标头(匿名):

Request
OPTIONS /api/items HTTP/1.1
Host: webapi.azurewebsites.net
Connection: keep-alive
Access-Control-Request-Method: GET
Access-Control-Request-Headers: accept, authorization
Origin: https://mvcapp.azurewebsites.net
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept: */*
Referer: https://mvcapp.azurewebsites.net/

Response
HTTP/1.1 302 Found
Content-Length: 504
Location: https://login.windows.net/{authority-guid}/oauth2/authorize?response_type=id_token&redirect_uri=https%3A%2F%2F....etc..etc.%2F&client_id={api-guid}&scope=openid+profile+email&response_mode=form_post&state=...etc...
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=4f51...snip....redact....db6d;Path=/;Domain=webapi.azurewebsites.net …

azure cors asp.net-web-api angularjs adal

7
推荐指数
1
解决办法
6522
查看次数

与ASP.NET Web Api 2.0一起使用时,Microsoft.Owin.Cors中间件究竟是什么?

我有一个带有令牌身份验证的ASP.NET Web Api 2.0项目,所有工作主要完成本文之后:

使用ASP.NET Web API 2,Owin和Identity,Bit Of Technology进行基于令牌的身份验证

但是我很难理解我的Startup.cs中的这行代码究竟是做什么的:

app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
Run Code Online (Sandbox Code Playgroud)

这不会使Web Api将Access-Control-Allow-Origin标头添加到我的API响应中,换句话说,它不会在我的Web Api中启用Cors(仍然试图了解如何执行此操作).它甚至没有将它添加到我的承载令牌认证服务器响应中.我必须将此代码提供给OAuthAuthorizationServerProvider:

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

在我的令牌提供者端点响应上启用Cors.

那么这个Microsoft.Owin.Cors中间件的用途是什么呢?因为我到处读到有关Web Api 2.0和Cors的这一行代码

app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
Run Code Online (Sandbox Code Playgroud)

过来:

cors owin asp.net-web-api2

6
推荐指数
1
解决办法
5567
查看次数

asp.net web api 2 CORS和身份验证授权配置

我已经创建了一个具有个人帐户安全性的asp.net web api 2服务.我试图按照这个例子将它称为AngularJs:http: //www.codeproject.com/Articles/742532/Using-Web-API-Individual-User-Account-plus-CORS-En 无法得到它工作所以从这里添加一些配置: 如何在WebAPI 2中进行CORS身份验证?

并且无法通过此错误:XMLHttpRequest无法加载'serverRegisterUrl'.'Access-Control-Allow-Origin'标头包含多个值'clientUrl,*,*',但只允许一个.因此,不允许来源'clientUrl'访问.

我不明白这个错误信息.我认为Access-Control-Allow-Origin字符串意味着允许原始clientUrl,所有标题,所有方法

我不明白这个问题.如果我应该只在某处指定原点,我不知道那是哪里.

我在Microsoft Azure上运行此操作,并使用vs express for web 2013 update 2以防万一.

不幸的是,我不得不从错误消息中取出我的链接,因为我需要至少在这里发布信誉10才能在一个问题中发布超过2个链接.

azure cors angularjs asp.net-web-api2

5
推荐指数
3
解决办法
6296
查看次数

如何在 asp.net web apis 中启用 cors 选项?

如何在asp.netWebApi 中启用 CORS 选项?我Microsoft.AspNet.WebApi.Cors从 nuget安装了用于 CORS 支持的软件包。但是,我收到一个错误,指出IAppBuilder不包含app.useCors?

c# cross-domain asp.net-web-api

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

在Web Api中使用邮递员授权进行属性身份验证

我正在使用RESTful服务,并发现Postman是GET,POST和测试API的最佳插件之一.

我在邮递员中找到了Basic Auth,No Auth,DIgest Auth,OAuth,AWS.如何测试授权控制器和方法.

我知道授权属性检查 user.Identity.IsAuthenticated

我不确定如何使用Postman在具有特定角色的控制器和方法中传递授权

[Authorize(Roles = "Admin, Super User")]

public ActionResult AdministratorsOnly()
{
    return View();
}
Run Code Online (Sandbox Code Playgroud)

这是我的启动文件

  public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }

    public static string PublicClientId { get; private set; }

    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context and user manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

        // Enable the application to use a cookie to …
Run Code Online (Sandbox Code Playgroud)

asp.net-web-api asp.net-web-api2 postman

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