问题是,我使用ASP.NET Core Identity来存储用户数据,但是登录时,它没有给我任何cookie或会话。它只是告诉我我已经成功登录,但是下次我要访问包含[Authorize]
属性的网页时,就无法访问。它将我重定向到登录页面。
if (ModelState.IsValid)
{
var user = await _userManager.FindByEmailAsync(model.Email);
if (user != null)
{
var result = await _signInManager.PasswordSignInAsync(user, model.Password, true, false);
if (result.Succeeded)
{
if (ReturnUrl != null)
{
return Redirect(ReturnUrl);
}
if (!string.IsNullOrEmpty(model.AppId))
{
return RedirectToAction(nameof(Authorize), new AuthorizeModel { AppId = model.AppId, RedirectUri = model.RedirectUri, State = model.State });
}
else
{
return Ok("You have successfully logged in");
}
...... More code
Startup.cs
services.AddDbContext<PassportDbContext>(options =>
{
options.UseSqlite(Configuration.GetConnectionString("DbConnection"));
});
services.AddIdentity<OAUser, IdentityRole>()
.AddEntityFrameworkStores<PassportDbContext>()
.AddDefaultTokenProviders();
-----------------------------------------------------
app.UseHttpsRedirection();
app.UseStaticFiles();
// app.UseCookiePolicy();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
Image: The cookies after I sign in
这意味着该应用程序不提供我已登录的任何凭据。
从.Net core 2.1开始内置支持GDPR(通用数据保护法规)。
并且直到您接受cookie,浏览器中才设置cookie添加以下代码以忽略GDPR
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Users/Login";
// ...
options.Cookie = new CookieBuilder
{
// ...
IsEssential = true // this cookie will always be stored regardless of the user's consent
};
});