【发布时间】:2021-10-16 03:22:40
【问题描述】:
技术栈
- 使用 .NET CORE React 模板
- 1 个 IIS 网站
- 应用程序池(v4 集成)
- 80 端口
- 单击注册按钮,调用注册组件。
- useEffect() 中的组件,使用 Axios 调用“/login URL”
- C# Map("/login") 被称为使用 LinkedIn 进行身份验证的挑战
- 然后返回 CORS 错误
错误 Snapshot 1 of 5 Snapshot 2 of 5; Snapshot 3 of 5; Snapshot 4 of 5; Snapshot 5 of 5
反应代码
const url = `/login`;
const headers = {
'Content-Type': 'text/html'
}
axios({
method: 'get',
url: url,
headers: headers
})
.then((response) => {...})
.catch((error: Error | AxiosError) => {...});
C# 代码 - 链接身份验证、Cookie、CORS 中间件
Start.cs - ConfigureServices()
public void ConfigureServices(IServiceCollection services)
{
#region AddAuthentication, AddLinkedIn, AddCookie
services.AddAuthentication()
.AddLinkedIn(o =>
{
IConfigurationSection linkedinAuthNSection =
Configuration.GetSection("Authentication:Linkedin");
o.ClientId = linkedinAuthNSection["ClientId"];
o.ClientSecret = linkedinAuthNSection["ClientSecret"];
})
.AddCookie(o =>
{
o.LoginPath = "/login";
o.LogoutPath = "/logout";
});
#endregion
#region Global CORS Policy Declaration
services.AddCors(o =>
{
o.AddDefaultPolicy(builder =>
builder.AllowAnyMethod()
.AllowAnyHeader()
.AllowAnyOrigin()
);
});
#endregion
services.AddControllersWithViews();
// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "client-app/build";
});
}
Start.cs - Configure()
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
#region Map/login
app.Map("/login", builder =>
{
builder.Run(async context =>
{
var properties = new AuthenticationProperties() { RedirectUri = "/" };
await context.ChallengeAsync("LinkedIn", properties);
});
});
#endregion
#region Map/logout
app.Map("/logout", builder =>
{
builder.Run(async context =>
{
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
context.Response.Redirect("/");
});
});
#endregion
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = Path.Join(env.ContentRootPath, "client-app");
if (env.IsDevelopment())
{
spa.Options.StartupTimeout = TimeSpan.FromSeconds(240);
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
}
【问题讨论】:
-
★ 我认为缺少的是如何将属性 [EnableCors] 链接到以下路径的属性:
app.Map("/login", builder => { MapLogin(builder); }); -
究竟是什么是 cors 错误? cors 错误在指出错误方面非常直接和准确
-
@KevinB 在开发工具中,它只在网络选项卡 (1) 中显示 CORS。 React 应用程序,在 IIS 中定义为端口 80 的“网站”。 (2) .NET Core REST API 定义为上述“网站”中的“应用程序”,位于不同的物理位置。 (3) React 向 REST API 发出“登录”请求。 (4) “/login”请求向LinkedIn发出一个Authentication Challenge。 (5) 然后我收到一个 CORS 错误
-
你用什么浏览器提供的信息这么少?
-
您应该将错误文本放在您的问题中。你被否决了,因为它不存在。谷歌根据其中的文本链接到问题,并且您链接以生成错误是完全短暂的,因为如果它得到修复,它将消失。还有其他一些小事,但这是大事。您的标题也没有帮助,也应该进行编辑以反映错误消息。如何提问->stackoverflow.com/help/how-to-ask
标签: reactjs typescript asp.net-web-api rest linkedin-api