ASP.NET Core 3.0身份不会向我的浏览器添加任何身份验证数据

问题描述 投票:0回答:1

问题是,我使用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

这意味着该应用程序不提供我已登录的任何凭据。

c# asp.net-mvc asp.net-core asp.net-core-identity asp.net-core-3.1
1个回答
0
投票

从.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
    };
});
© www.soinside.com 2019 - 2024. All rights reserved.