Серверная часть Asp.net core 3.1 не получает идентификатор из файла cookie

У меня есть интерфейс vue.js и серверная часть asp.net core 3.1. Бэкенд использует SignInManager и Identity. Я пытаюсь использовать аутентификацию cookie. Запросы API работают из ролей Postman(!!), применяются все, но не из приложения vue (httpContext.User.Identity.IsAuthenticated имеет значение false). Идентификация пуста. Cookie присутствует в HttpContext

        public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<MyDBContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:Default"]));
        services.AddCors();
        services.AddControllers()
        services.AddIdentity<AppUser, IdentityRole>(
            opts =>
            {
                opts.SignIn.RequireConfirmedEmail = true;
            }
            )
            .AddSignInManager<SignInManager<AppUser>>()
            .AddEntityFrameworkStores<MyDBContext>()
            .AddDefaultTokenProviders();

        services.ConfigureApplicationCookie(options =>
        {
            // Cookie settings
            options.Cookie.HttpOnly = true;
            options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
            options.Cookie.SameSite = SameSiteMode.None;
            options.SlidingExpiration = true;
        });

       //some DI
       ...
       //
       }

еще немного Startup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, UserManager<AppUser> userManager)
    {
        app.UseRouting();
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseCors(x => x
            .AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowAnyHeader());
        app.UseHttpsRedirection();


        app.UseAuthentication();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });

    }

Я пытался проксировать запросы к API в приложении vue. Не помогло

module.exports = {
devServer: {
   proxy: {
     '^/api': {
       target: 'https://localhost:44376',
       ws: true,
       changeOrigin: true
     }
   }
  }
}

Что может быть не так?


person Alexey Korsakov    schedule 15.01.2020    source источник


Ответы (1)


Может быть, вам не хватает AddRoles<IdentityRoles>()?

services.AddIdentity<AppUser, IdentityRole>(
    opts =>
    {
        opts.SignIn.RequireConfirmedEmail = true;
    }
)
.AddRoles<IdentityRoles>() .// <== this line
.AddSignInManager<SignInManager<AppUser>>()
.AddEntityFrameworkStores<MyDBContext>()
.AddDefaultTokenProviders();
person Bemn    schedule 21.07.2020