我是新的asp.net核心和角,我有一些问题的CORS。我搜索,我几乎达到了我的目标,但我仍然有一个问题的具体情况。
以下是以下步骤:
我从角度模板开始,用这篇文章进行描述。
我创建了一个非常简单的api控制器来检索当前用户的角色,下面是我的简化代码:
[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 :
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,并且它运行良好(使用了授权属性),但是我不能对所有的方法都这样做!
知道吗?
发布于 2019-01-08 10:02:49
最后,经过很长时间的询问,我的项目角或asp.net核心,我去看我的项目api,我发现只有本地主机: 4200是授权的原产地!为了寻找一个简单的失踪配置而迷失了几个小时。格里尔
https://stackoverflow.com/questions/54040999
复制相似问题