简体   繁体   English

Asp.net core 3.1 后端无法从 cookie 中获取身份

[英]Asp.net core 3.1 backend does not get the Identity from cookie

I have vue.js frontend and asp.net core 3.1 backend.我有 vue.js 前端和 asp.net core 3.1 后端。 Backend uses SignInManager and Identity.后端使用 SignInManager 和 Identity。 I am trying to use cookie authentication.我正在尝试使用 cookie 身份验证。 Api requests work from Postman(!!) roles are applied, everything, but do not from vue app (httpContext.User.Identity.IsAuthenticated is false).应用来自 Postman(!!) 角色的 Api 请求工作,一切都适用,但不要来自 vue 应用程序(httpContext.User.Identity.IsAuthenticated 为假)。 Indentity is empty.身份为空。 Cookie is present in the HttpContext 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
       ...
       //
       }

bit more Startup.cs更多 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();
        });

    }

I was trying to proxy requests to api in vue app.我试图在 vue 应用程序中代理对 api 的请求。 Did not help没有帮助

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

What can be wrong?有什么问题?

Maybe you are missing AddRoles<IdentityRoles>() ?也许您错过了AddRoles<IdentityRoles>()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PUT 请求抛出 403 CORS 错误 Asp.Net Core 3.1 + Vue3 +IIS10 - PUT requests are throwing 403 CORS error Asp.Net Core 3.1 + Vue3 +IIS10 为 VueJS SPA 和 asp.net 核心 3.1 实现 OAuth2/OIDC - Implementing OAuth2/OIDC for VueJS SPA and asp.net core 3.1 如何使用Identity Server 4保护Asp.net核心2.1和Vue.js单页面应用程序 - How to protect Asp.net core 2.1 and Vue.js single page application with Identity Server 4 vuejs 历史模式路由问题与路由参数、asp.net 核心和 SignalR 后端 - vuejs history mode routing issue with routing parameters, asp.net core and SignalR backend ASP.Net Core 2.1 Vue.JS SPA - Cookie Auth和Vue.JS Dev Server - ASP.Net Core 2.1 Vue.JS SPA - Cookie Auth and the Vue.JS Dev Server Asp.net Core Web API - Downloading multiple images from blob storage to zip file (via Axios get) - Asp.net Core Web API - Downloading multiple images from blob storage to zip file (via Axios get) 从 ASP.NET Web API 后端生成 + 发送 .xlsx 文件到 Vue.js 前端卡住 - Stuck generating + sending .xlsx file from ASP.NET Web API backend to Vue.js frontend HMR 不适用于 vue 和 asp.net 核心 - HMR not working with vue and asp.net core 使用 ASP.NET Core + VueJS 上传文件 - File upload with ASP.NET Core + VueJS 从GoogleAPI重定向时,ASP.NET Core“ oauth状态丢失或无效” - ASP.NET Core 'The oauth state was missing or invalid' while redirecting from GoogleAPI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM