【发布时间】:2021-11-02 19:14:45
【问题描述】:
我正在使用 ASP.NET Core 和 Angular 编写一个 SPA 应用程序。我正在使用 CORS 在前端和后端之间进行通信。我发现我登录后无法获得授权,并且客户端没有cookie。我已经卡了2天了,请帮助我。
在startup.cs,我有:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(...)
services.AddDbContext<TaskMatrixContext>(...);
services.AddControllers(...)
services.Configure<IdentityOptions>(options =>
{...});
services.AddIdentity<AppUser,IdentityRole().
AddEntityFrameworkStores<TaskMatrixContext>();
services.AddAuthentication().AddCookie();
services.ConfigureApplicationCookie(options =>
{...});
services.AddAuthorization(options =>
{...});
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseRouting();
app.UseCors("AllowMyOrigin");
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
这是我的AccountController:
[HttpPost("/login")]
public async Task<ActionResult<LoginDto>> Login(LoginDto model)
{
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, true);
if (result.Succeeded)
{
model.IsLoggedIn = true;
}
if (result.IsLockedOut)
{
model.IsLockedOut = true;
model.RedirectUrl = "/account/lockedout";
}
}
return model;
}
在我的前端:
login(dto:LoginInput):Observable<LoginResult> {
return this.http.post<LoginResult>
(this.baseUrl+'/login',dto).pipe(
tap(...)
);
}
【问题讨论】:
标签: c# angular asp.net-core cookies asp.net-core-identity