【发布时间】:2019-10-13 12:05:15
【问题描述】:
我正在使用 IDE (Visual Studio 2019) 在 dotnet core 3.0 中为 Angular v8.0 SPA 应用程序使用“新”项目模板。
我要做的是在首次加载应用程序之前保护 SPA 本身。这意味着:当我打开我的 SPA 时,例如https://localhost:44318/ 我想立即被重定向到授权服务器,而不是单击一些将进行身份验证的按钮。
查看项目结构:
我已经尝试过的:
//Added this to redirect to Identity Server auth prior to loading SPA
app.Use(async (context, next) =>
{
if (!context.User.Identity.IsAuthenticated)
{
await context.ChallengeAsync("Identity.Application");
}
else
{
await next();
}
});
上面我在app.UseSpa之前添加的行
我的 Startup.cs:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
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.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
services.AddAuthentication()
.AddIdentityServerJwt();
services.AddControllersWithViews();
services.AddRazorPages();
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
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();
if (!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}
app.UseRouting();
app.UseAuthentication();
app.UseIdentityServer();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
app.Use(async (context, next) =>
{
if (!context.User.Identity.IsAuthenticated)
{
await context.ChallengeAsync("Identity.Application");
}
else
{
await next();
}
});
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");
}
});
}
}
当前行为:
当我运行我的应用程序时,我会立即重定向到我的授权服务器并路由到 https://localhost:44318/Identity/Account/Login?ReturnUrl=%2F,但我无法路由到我的 SPA 路由和页面。当我单击 XYZ 上的锚链接时,理想情况下它是主组件上的路由,或者如果我强制从 url 路由计数器组件,它会向我显示以下相同的登录页面。请帮助我解决我做错了什么以及在首次加载之前通过授权服务器保护 SPA 的正确方法。
输出
【问题讨论】:
-
如果我的问题是正确的,你有一个 SPA 并且用户要查看索引页面,他们必须获得授权。现在您已经实现了,但是在单击登录按钮后,该页面仍会加载身份验证页面。 1.您是否从服务器接收令牌? 2.您是否将令牌保存到语言环境? 3. 你的应用能识别令牌吗?
-
当点击登录按钮时,它会验证我的身份并显示我的电子邮件,但是当我将自己路由到 SPA 组件时,它不允许我并把我带到登录页面上,但我仍然' m 认证成功。
标签: c# angular asp.net-core .net-core