对每个请求(包括 js / spa)进行 .NET Core 身份验证
.NET Core authentication on every request (including js / spa)
我在 .NET 核心解决方案中有一个 React 项目,基本上我只希望用户经过身份验证才能查看 React 项目/SPA。
经过大量调查,如果不在 React 项目中执行一些代码,这似乎是不可能的,这意味着向可以在客户端中操作的 JS 添加安全性。
我发现可以对 .Net Core 中的每个请求进行身份验证,这可能是最好的方法。这可能吗?
我使用了其他资源的一些验证码,但出现错误。这是我的 startup.cs 代码
public class 启动
{
public Startup(IConfiguration配置)
{
配置=配置;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/build";
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
// This produces a server error.
app.Use(async (context, next) =>
{
if (!context.User.Identity.IsAuthenticated)
{
await context.ChallengeAsync("");
}
else
{
await next();
}
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
//routes.MapSpaFallbackRoute(
// name: "spa-fallback",
// defaults: new { controller = "Home", action = "AuthorizedSpaFallBack" });
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
}
}
或者有没有人有任何其他想法来实现我想要的?
谢谢
如果我没理解错的话,您想使用后端身份验证服务对访问 SPA 的用户进行身份验证。在那种情况下,您可以求助于 IdentityServer。
下面的 link 演示了 API 授权与 ASP.NET 用于验证和存储用户的核心身份以及用于实现 OpenID Connect 的 IdentityServer 的结合。
我在 .NET 核心解决方案中有一个 React 项目,基本上我只希望用户经过身份验证才能查看 React 项目/SPA。
经过大量调查,如果不在 React 项目中执行一些代码,这似乎是不可能的,这意味着向可以在客户端中操作的 JS 添加安全性。
我发现可以对 .Net Core 中的每个请求进行身份验证,这可能是最好的方法。这可能吗?
我使用了其他资源的一些验证码,但出现错误。这是我的 startup.cs 代码
public class 启动 { public Startup(IConfiguration配置) { 配置=配置; }
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/build";
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
// This produces a server error.
app.Use(async (context, next) =>
{
if (!context.User.Identity.IsAuthenticated)
{
await context.ChallengeAsync("");
}
else
{
await next();
}
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
//routes.MapSpaFallbackRoute(
// name: "spa-fallback",
// defaults: new { controller = "Home", action = "AuthorizedSpaFallBack" });
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
}
}
或者有没有人有任何其他想法来实现我想要的?
谢谢
如果我没理解错的话,您想使用后端身份验证服务对访问 SPA 的用户进行身份验证。在那种情况下,您可以求助于 IdentityServer。
下面的 link 演示了 API 授权与 ASP.NET 用于验证和存储用户的核心身份以及用于实现 OpenID Connect 的 IdentityServer 的结合。