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,
    

相关内容

热门资讯

2027版外挂!新呼啦麻将外挂... 2027版外挂!新呼啦麻将外挂多功能AI工具,新呼啦麻将果然是有外挂工具亲,关键说明,新呼啦麻将透视...
WPK四人开挂方针!wepok... WPK四人开挂方针!wepoker-h5下载,红龙PoKerai多功能开挂工具1、下载好wepoke...
记者爆料透视!四方河南麻将ai... 记者爆料透视!四方河南麻将ai透视多功能插件,竟然有透视技巧手段AI 是一款可以让一直输的玩家修复黑...
WPK辅助器辅助教程书!wep... WPK辅助器辅助教程书!wepoker免费脚本咨询,智星菠萝德州ai多功能辅助脚本1、打开软件启动之...
2027版外挂!四川自贡花麻将... 2027版外挂!四川自贡花麻将外挂多功能AI工具,四川自贡花麻将总是有外挂工具1、玩家可以在四川自贡...
玩家交流透视!瑞星游戏ai透视... 您好:瑞星游戏这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,...
WPK透视版透视挂教材!wep... WPK透视版透视挂教材!wepoker提高好牌率,德扑之星ai多功能透视挂方法1、进入游戏-大厅左侧...
一分钟快速了解辅助!微乐三丁拐... 很多人使用云端 AI 时,都担心微乐三丁拐、内部资料上传带来隐私风险。这款本地运行的 AI 黑科技,...
2027版开挂!凑一桌麻将开挂... 2027版开挂!凑一桌麻将开挂多功能AI工具,凑一桌麻将本来有开挂工具凑一桌麻将能透视中分为三种模型...
WPK软件透视模板!wepok... WPK软件透视模板!wepoker透视苹果系统,wpkai多功能透视神器1)wepoker透视苹果系...