客户端无法获取带有身份的 cookie
Client can't get cookies with Identity
我正在使用 ASP.NET 核心和 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(...)
);
}
您应该使用 withCredentials
选项:
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
在您的前端:
login(dto:LoginInput):Observable<LoginResult> {
return this.http.post<LoginResult>
(this.baseUrl+'/login',dto,{withCredentials: true}).pipe(
tap(...)
);
}
对于需要身份验证 cookie 的每个请求都应执行相同的操作
又研究了几天,发现不能用Identity Core在REST中实现认证Api。我们应该使用 JWT 来生成令牌并验证它。推荐的前端教程是:
https://jasonwatmore.com/post/2019/10/14/aspnet-core-3-simple-api-for-authentication-registration-and-user-management
对于后端:
https://dev.to/moe23/asp-net-core-5-rest-api-authentication-with-jwt-step-by-step-140d
我正在使用 ASP.NET 核心和 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(...)
);
}
您应该使用 withCredentials
选项:
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
在您的前端:
login(dto:LoginInput):Observable<LoginResult> {
return this.http.post<LoginResult>
(this.baseUrl+'/login',dto,{withCredentials: true}).pipe(
tap(...)
);
}
对于需要身份验证 cookie 的每个请求都应执行相同的操作
又研究了几天,发现不能用Identity Core在REST中实现认证Api。我们应该使用 JWT 来生成令牌并验证它。推荐的前端教程是: https://jasonwatmore.com/post/2019/10/14/aspnet-core-3-simple-api-for-authentication-registration-and-user-management 对于后端: https://dev.to/moe23/asp-net-core-5-rest-api-authentication-with-jwt-step-by-step-140d