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,
    

相关内容

热门资讯

最新技巧"一起宁德福... 最新技巧"一起宁德福鼎打炸辅助"开挂(透视)聚星ai辅助工具收费多少(真的有挂) 【无需打开直接搜索...
终于懂了"久久互娱科... wepoker脚本下载是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以...
推荐一款"大唐山西辅... hhpoker德州透视挂是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可...
透视讲解"蜀山四川小... 哈糖大菠萝有挂吗5个常用方法是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用...
1.9分钟了解"小南... 1.9分钟了解"小南娱乐科技"开挂(透视)poker红龙辅助(了解有挂);无需打开直接搜索微信(13...
推荐一款"兴动互娱绥... 【亲,兴动互娱绥化辅助器挂 这款游戏可以开挂的,确实是有挂的,很多玩家在这款兴动互娱绥化辅助器挂中打...
每日必看推荐"德州透... >>您好:德州透视是真的假的确实是有挂的,很多玩家在这款德州透视是真的假的游戏中打牌都会发现很多用户...
透视玄学"微乐家乡a... wepoker私人局外卦 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理...
今日公布"闽游十三水... hhpoker为什么一直输是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户...
玩家攻略推荐"随意玩... wepoker代打辅助机器人 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四...