AngularJS资源选项withCredentials问题

时间:2015-09-29 18:15:47

标签: javascript c# asp.net angularjs asp.net-core

我遇到使用AngularJS资源能够,PUT,POST或DELETE的问题。但GET工作得很好。

最终我有一个.NET Web API,我的服务器端我启用了CORS,但我使用Windows身份验证来授权用户。

在我的app.Config for angular上我添加了withCredentials:

app.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    $httpProvider.defaults.withCredentials = true;
}
]);

在我的AngularJS控制器中我有这样的东西:

var ModelGrab = $resource($rootScope.baseurl + 'Staging/GIS/CurrentTransformer/:id', null, { 'delete': { method: 'DELETE' } });

现在我打电话给GET这个函数是这样的:

ModelGrab.get({ page: $scope.currentPage, records: $scope.recordsperpage, sort: $scope.sortType, reverse: $scope.sortReverse, search: $scope.search, all: false }).$promise.then(function (model) {
        $rootScope.model = model.List;
        $scope.totalItems = model.Count;
        setTotalPages();
    }, function (errResponse) {
        $scope.showMessage('Error retrieving the model', 'danger');
    });

一切都很好。但是当我像这样调用DELETE函数时:

ModelGrab.delete({ 'ID': ID }, function () {
                    $scope.showMessage('Data saved successfully', 'success');
                    GetData();
                }, function (e) {
                    $scope.showMessage('An error occurred while trying to save the data', 'danger');
                });

我收到错误:

选项http://localhost:32200/api/Staging/GIS/CurrentTransformer?ID=1 http://localhost:32200/api/Staging/GIS/CurrentTransformer?ID=1。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原点“http://localhost:27062”访问。响应的HTTP状态代码为401。

现在,当我查看网络信息以查看它是否通过DELETE操作的withCredentials时,我注意到它没有通过它。

在我的ASP.NET启动中,我已经设置了CORS,所以我应该很高兴去接受它:

var policy = new Microsoft.AspNet.Cors.Core.CorsPolicy();

        policy.Headers.Add("*");
        policy.Methods.Add("*");
        policy.Origins.Add("http://localhost:27062");
        policy.SupportsCredentials = true;

        services.ConfigureCors(x => x.AddPolicy("ADMSCORS", policy));
        services.AddEntityFramework().AddSqlServer().AddDbContext<ADMSContext>(options => options.UseSqlServer(Configuration.GetSection("Data:ConnectionString").Value));

        services.Configure<AuthorizationOptions>(options =>
        {
            options.AddPolicy("ADMSServices", new AuthorizationPolicyBuilder().RequireRole(Configuration.GetSection("Security:Role").Value).Build());
        });

在我的Startup配置中,我添加了:

app.UseCors("ADMSCORS");

在我的控制器中,我有以下内容:

[Route("api/Staging/GIS/CurrentTransformer")]
[Authorize(Policy = "ADMSServices")]
public class CurrentTransformerController : Controller
{
    [FromServices]
    public ADMSContext _Context { get; set; }
    [HttpGet]
    public ListObject Get(int page, int records, string sort, bool reverse, string search, bool all)
    {
        ListObject model = new ListObject();
        model.List = GetList(page, records, sort, reverse, search, all);

        model.Count = _Context.CurrentTransformers.Count();
        return model;
    }

    [HttpDelete]
    public void Delete(int id)
    {
        var model = _Context.CurrentTransformers.SingleOrDefault(line => line.ID == id);
        if (model != null)
        {
            _Context.CurrentTransformers.Remove(model);
            _Context.SaveChanges();
        }
    }
}

我只需要弄清楚为什么我会在PUT,POST&amp;上收到错误。删除。为什么不通过命令传递Windows身份验证?

0 个答案:

没有答案