ASP.NETCore2.1MVC-无法设置从webapi响应接收到的cookie
创始人
2024-09-17 09:00:43
0
  1. 在 Startup.cs 中确保已启用 cookie 必要的中间件:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.Configure(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options =>
            {
                options.AccessDeniedPath = "/AccessDeniedPath";
                options.LoginPath = "/Login";
                options.Cookie.HttpOnly = true;
                options.Cookie.Name = "MyApplicationCookie";
                options.Cookie.SecurePolicy = CookieSecurePolicy.None;// set to None if you're not using SSL
                options.Cookie.SameSite = SameSiteMode.None;// Only set to None if you're not using HTTPS
                options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
            });
    }
    
  2. 确保 web api 响应将 cookie 设置为 SameSiteMode=None,以确保在授权操作期间不会出现问题

    [HttpGet]
    public async Task Login(string returnUrl = null)
    {
        returnUrl = returnUrl ?? Url.Content("~/");
    
        var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
        if (result.Succeeded)
        {
            _logger.LogInformation("User logged in.");
            var user = await _userManager.FindByEmailAsync(Input.Email);
    
            var roles = await _userManager.GetRolesAsync(user);
    
            var claims = new List
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id),
                new Claim(ClaimTypes.Name, user.UserName),
                new Claim(ClaimTypes.Email, user.Email),
            };
    
            foreach (var role in roles)
            {
                claims.Add(new Claim(ClaimTypes.Role, role));
            }
    
            var claimsIdentity = new ClaimsIdentity(
                claims, CookieAuthenticationDefaults.AuthenticationScheme);
    
            var authProperties = new AuthenticationProperties
            {
                IsPersistent = Input.RememberMe,
    

相关内容

热门资讯

透视肯定(wepoke确实有挂... 透视肯定(wepoke确实有挂)fishpoker大菠萝外挂(详细辅助透明教程)原本有挂1)wepo...
微扑克ai机器人!wepoke... 微扑克ai机器人!wepoke软件透明挂,(wepOke)从来存在有挂(详细透视wpk教程)是一款可...
透视智能ai(wepokeai... 透视智能ai(wepokeai机器人)微扑克有挂(详细辅助扑克教程)都是是真的有挂,您好,wepok...
wepoke一定有挂!wepo... wepoke一定有挂!wepoke辅助机器人,(AAPoKER)一直有挂(详细透视辅助教程)科技教程...
透视好友房(wepoke真的有... 透视好友房(wepoke真的有挂)德扑ai智能机器人线上代打(详细辅助科技教程)好像是有挂是一款可以...
透视代打(微扑克游戏辅助器)w... 透视代打(微扑克游戏辅助器)wpk ai(详细辅助力荐教程)真是真的是有挂是一款可以让一直输的玩家,...
wepoke辅助技巧!德州之星... wepoke辅助技巧!德州之星辅助透视,(aapokEr)果然真的是有挂(详细辅助实用技巧);《WP...
透视模拟器(WePoKe透明挂... 透视模拟器(WePoKe透明挂)微扑克辅助钻石(详细辅助2025新版总结)总是是有挂1)WePoKe...
微扑克全自动机器人!wepok... 微扑克全自动机器人!wepoke ai,(wEPOKE)本然是有挂(详细透视插件教程)1、点击下载安...
透视透视挂(aapoker辅助... 透视透视挂(aapoker辅助工具存在)aapoker软件有猫腻吗(详细辅助wpk教程)一贯是有挂a...