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

标签 c# asp.net-mvc asp.net-core asp.net-core-identity asp.net-core-3.1

问题是我使用 ASP.NET Core Identity 来存储用户数据,但是当我登录时,它没有给我任何 cookie 或 session 。它只是告诉我我已经成功登录,但下次我想访问包含 [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.Configure<CookiePolicyOptions>(options =>
            {
                options.ConsentCookie.IsEssential = true;
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => false;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
 app.UseCookiePolicy();

关于c# - ASP.NET Core 3.0 身份不向我的浏览器添加任何身份验证数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59598352/

相关文章:

c# - 如何正确处理泛型类中的错误?

asp.net-mvc - ASP.NET MVC ModelMetadata - 当我们在模型上放置属性来描述 UI 时,我们是否违反了关注点分离?

c# - 正则表达式以 "AB"或 "EX"开头,仅使用字母数字值

c# - 从 ASP.NET Web API ASP.NET Core 2 返回 HTML 并获取 http 状态 406

c# - 使用 StackExchange.Redis 库时对 Redis 的超时和缓慢请求

c# - 使用 CultureInfo 时 DateTime.Parse 返回错误值

c# - 如何正确保留标识值以供在数据库中使用?

c# - 这个永久的 SQLExpress 连接字符串来自哪里(不是 web.config)?

asp.net-mvc - ASP MVC 多下拉列表搜索表单

c# - NPOI Mapper - map 列表属性