首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否有特定的asp.net核心配置,使CORS能够在多个APIs的角度上工作?

是否有特定的asp.net核心配置,使CORS能够在多个APIs的角度上工作?
EN

Stack Overflow用户
提问于 2019-01-04 14:36:25
回答 1查看 130关注 0票数 0

我是新的asp.net核心和角,我有一些问题的CORS。我搜索,我几乎达到了我的目标,但我仍然有一个问题的具体情况。

以下是以下步骤:

我从角度模板开始,用这篇文章进行描述。

我创建了一个非常简单的api控制器来检索当前用户的角色,下面是我的简化代码:

代码语言:javascript
运行
复制
[Authorize()]
[Route("api/[controller]")]
public class PermissionController : Controller
{
    private List<Tuple<string, string>> roles = new List<Tuple<string, string>> { new Tuple<string, string>("Admin", "Role_AutHab_Dev_Administrateur"), new Tuple<string, string>("Gest", "Role_AutHab_Dev_Gestionnaire") };

    [HttpGet]
    public IEnumerable<string> Get()
    {
        bool test = false;
        List<string> lst = new List<string>();

        foreach (var item in roles)
        {
            test = User.IsInRole(item.Item2);
            if (test)
                lst.Add(item.Item1);
        }

        return lst.ToArray();
    }

To solved [this problem](https://github.com/aspnet/CORS/issues/60) with CORS in ASPNet Core, I allowed anonymous mode and windows mode in my c# project properties and configure my startup class like this :

代码语言:javascript
运行
复制
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
    }

    app.UseStaticFiles();
    app.UseSpaStaticFiles();
    app.UseCors(o => o.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller}/{action=Index}/{id?}");
    });

    app.UseSpa(spa =>
    {
        // To learn more about options for serving an Angular SPA from ASP.NET Core,
        // see https://go.microsoft.com/fwlink/?linkid=864501

        spa.Options.SourcePath = "ClientApp";

        if (env.IsDevelopment())
        {
            spa.UseAngularCliServer(npmScript: "start");
        }
    });
}

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors();

    services.AddAuthentication(Microsoft.AspNetCore.Server.IISIntegration.IISDefaults.AuthenticationScheme);

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

    // In production, the Angular files will be served from this directory
    services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = "ClientApp/dist";
    });
}

在这一点上,一切正常工作。

当我使用另一个为我提供业务数据的API时,问题就出现了。当我使用IIS Express (使用visual studio)或在发布后使用IIS运行我的项目时,在chrome console选项卡中出现CORS错误的查询失败,但在chrome网络选项卡中运行良好!

就像这样:

我认为这个问题来自于匿名模式,因为我试图通过权限控制器调用我的第二个API,并且它运行良好(使用了授权属性),但是我不能对所有的方法都这样做!

知道吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-08 10:02:49

最后,经过很长时间的询问,我的项目角或asp.net核心,我去看我的项目api,我发现只有本地主机: 4200是授权的原产地!为了寻找一个简单的失踪配置而迷失了几个小时。格里尔

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54040999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档