Asp.net核心:托管具有相同身份验证的MVC和SPA。重定向到应用程序的MVC部分时客户端注销

时间:2020-08-05 21:21:02

标签: c# asp.net angular asp.net-mvc

我使用默认的具有身份的asp.net核心+角形支架将我的SPA托管在MVC应用程序中,身份验证部分的工作原理与应在角形SPA内一样。

我的startup.cs没什么特别的,标准的.net核心支架材料:

select
    extract(year from justify_interval(min_age::interval)) min_age,   
    extract(year from justify_interval(max_age::interval)) max_age
from tbl

我还通过.net核心控制器为应用程序的不同部分提供了剃刀视图,但是脚手架式身份验证在这里根本不起作用。

这是当前发生的情况:

  1. 转到地址,受保护的角形路由器防护将我重定向到脚手架登录页面。
  2. 我登录并重定向到SPA。”

当我从SPA中“遍历”路径时,会出现问题:

  1. 我转到:http:// myadress / mycontroller / MyAction)我退出了应用程序,并使用带有public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer( Configuration.GetConnectionString("DbName"))); services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores<ApplicationDbContext>(); services.AddIdentityServer() .AddApiAuthorization<ApplicationUser, ApplicationDbContext>(); services.AddAuthentication() .AddIdentityServerJwt(); services.AddControllersWithViews().AddRazorRuntimeCompilation(); services.AddRazorPages(); services.AddHttpContextAccessor(); // In production, the Angular files will be served from this directory services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; }); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Error"); 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.UseSpa(spa => { spa.Options.SourcePath = "ClientApp"; if (env.IsDevelopment()) { spa.UseProxyToSpaDevelopmentServer("http://localhost:4200"); } }); } } 的简单控制器来检查HttpContext.User是否存在:
UserManager<ApplicationUser>

我尝试放置 public IActionResult MyAction() { //user cannot be found here. var user = _userManager.GetUserAsync(HttpContext.User); return View(); } ,以为再次重定向到登录页面应该可以帮助我登录到应用程序的“ Mvc部分”。 (因为之前我使用redirectURL登录到SPA),但是没有成功:我在访问时得到401,但没有更多信息。

对于整个这场灾难,我希望得到的结果如下:

  • asp.net核心中托管的应用程序的2个部分; 1是您的标准MVC应用程序,具有剃刀视图(仅管理员),另一种是SPA(面向客户端)(仅限客户端)。
  • 一旦用户登录,他/她将根据角色正确重定向到应用的右侧,因此/ dashboard用于管理员(MVC),而/ app用于客户端(角度)

基本上就是这样,我不认为这应该没有那么困难,但是对于asp.net核心来说并不是什么新鲜事。

希望我能提供足够的信息,对您的帮助表示感谢。

1 个答案:

答案 0 :(得分:0)

您将需要在MVC,Web API和SPA之间共享相同的无状态授权Cookie。

我建议使用.NET HttpOnly cookie。

最初加载SPA时,您可以查询Web API以获取用户权限。如果cookie存在,它将随请求一起发送,您可以返回用户权限,否则不返回任何内容-这表示用户尚未登录。

我已经使用React SPA + Core Web API(用于管理)和Core MVC(用于服务器端渲染的前端)成功地做到了这一点。