CORS does not work MVC6 - RC1

CORS is enabled in the MVC6 application, and the launch looks like this:

I added the following code to the method ConfigureServices():

services.AddCors(options =>
    {
        options.AddPolicy("AllowSpecificOrigin",
            builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials());
    });

...... In configuration Configure()

app.UseCors("AllowSpecificOrigin");

.......

My code is trying to download angular patterns from a different url, templtes get the download (saw in the violin). But my browser console says

XMLHttpRequest cannot load http: // hsa-is-utl32. [Mydomain] .com / LayoutService // platform / templates / layout / topbanner.html . The requested resource does not have an Access-Control-Allow-Origin header. Origin ' http: // mydevbox. [Mydomain] .com ' is therefore not allowed.

There is no clue what is wrong?

+4
3

. 100% , , , , . , , http://myapp.domain/, , http://templates.domain/template.html, . , .

CORS MVC 6 (myapp.domain).

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy("AllowSpecificOrigin",
                builder => builder.WithOrigins("http://myapp.domain").AllowAnyHeader().AllowAnyMethod().AllowCredentials());
    });

    services.AddMvc();
}

public void Configure(IApplicationBuilder app)
{
    app.UseIISPlatformHandler();
    app.UseDefaultFiles();
    app.UseStaticFiles();
    app.UseCors("AllowSpecificOrigin");
    //make sure its before UseMvc()!!
    app.UseMvc();
}

web.config http://templates.domain http://templates.domain, CORS http://myapp.domain/. , angular .

<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="http://myapp.domain" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

, , , - , :

(function () {
    "use strict";

    var app = angular.module("myApp", ['ngRoute']);

    // configure the routes
    app.config(['$provide', '$sceDelegateProvider', '$routeProvider', '$locationProvider', '$httpProvider',
        function ($provide, $sceDelegateProvider, $routeProvider, $locationProvider, $httpProvider) {

            $sceDelegateProvider.resourceUrlWhitelist(['self', 'http://templates.domain/**']);
            $httpProvider.defaults.headers.common['Access-Control-Allow-Origin'] = true;
            $httpProvider.defaults.useXDomain = true;

            //routes
            $routeProvider.when('/', {
                templateUrl: 'http://templates.domain/template.html',
                controller: 'templatesController'
            })
            .otherwise({ redirectTo: '/' });

            $locationProvider.html5Mode(true);

            $httpProvider.defaults.headers.common = {
                'Content-Type': 'application/json, charset=UTF-8'
            };
        }]);

    app.controller('templatesController', ['$scope', function ($scope) {
        $scope.message = 'message from controller';
    }]);

})();

html <div ng-view></div> angular-routes.

angular.

Access-Control-Allow-Origin: http://myapp.domain/

, .

+2

web.config . , IIS Express. app.UseCors( "AllowSpecificOrigin" ) Startup.cs.

<configuration>
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <add name="Access-Control-Allow-Origin" value="http://localhost:10001" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>
+1

, MVC. , AllowCredentials, Access-Control-Allow-Origin = *.

MVC ( ).

.AllowCredentials());

creadentials, , URL- Access-Control-Allow-Origin = ' '

Fiddler , .

0

Source: https://habr.com/ru/post/1624638/


All Articles