Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在每次blazor代码更改和重新编译后停止cookie失效?_C#_Blazor_Blazor Server Side_Blazor Webassembly - Fatal编程技术网

C# 如何在每次blazor代码更改和重新编译后停止cookie失效?

C# 如何在每次blazor代码更改和重新编译后停止cookie失效?,c#,blazor,blazor-server-side,blazor-webassembly,C#,Blazor,Blazor Server Side,Blazor Webassembly,我正在使用托管Blazor WebAssembly项目模板,在将Cookie设置为主要身份验证方法(也适用于API)之后,一切都很好,直到我更改了一行代码(前端或后端),自动编译开始,网页重新加载(感谢“无调试启动”),但这一次,auth cookie似乎无效,我似乎不再登录 有没有办法阻止这种行为?还是一种阻止cookie身份验证在不同编译之间更改的方法 以下是我的CookieAuthenticationEvents实现/覆盖: public class CookieAuth : Cookie

我正在使用托管Blazor WebAssembly项目模板,在将Cookie设置为主要身份验证方法(也适用于API)之后,一切都很好,直到我更改了一行代码(前端或后端),自动编译开始,网页重新加载(感谢“无调试启动”),但这一次,auth cookie似乎无效,我似乎不再登录

有没有办法阻止这种行为?还是一种阻止cookie身份验证在不同编译之间更改的方法

以下是我的CookieAuthenticationEvents实现/覆盖:

public class CookieAuth : CookieAuthenticationEvents
{
    readonly IUserService UserRepository;

    public CookieAuth(IUserService userRepository)
    {
        UserRepository = userRepository;
    }

    public override Task RedirectToLogin(RedirectContext<CookieAuthenticationOptions> context)
    {
        context.Response.StatusCode = StatusCodes.Status401Unauthorized;
        return Task.CompletedTask;
    }
    public override Task RedirectToAccessDenied(RedirectContext<CookieAuthenticationOptions> context)
    {
        context.Response.StatusCode = StatusCodes.Status401Unauthorized;
        return Task.CompletedTask;
    }
    public override async Task ValidatePrincipal(CookieValidatePrincipalContext context)
    {
        var userPrincipal = context.Principal;

        var userId = (from c in userPrincipal.Claims
                                    where c.Type == ClaimTypes.NameIdentifier
                                    select c.Value).FirstOrDefault();

        var isTeacherAndBlocked = await UserRepository.IsTeacherAndIsBlocked(userId);

        if (isTeacherAndBlocked)
        {
            context.RejectPrincipal();
            await context.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            return;
        }

        var lastChanged = (from c in userPrincipal.Claims
                                             where c.Type == "LastChanged"
                                             select c.Value).FirstOrDefault();

        if (!DateTime.TryParse(lastChanged, out DateTime lastChangedDate))
        {
            context.RejectPrincipal();
            await context.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            return;
        }

        if (!await UserRepository.ValidateLastChanged(lastChanged, userId))
        {
            context.RejectPrincipal();
            await context.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
        }
    }
}

没关系,我发现这是一个比较日期作为字符串的问题,只是不要这样做,用记号D

Auth cookie可以在重建后继续存在(我的总是这样),因此必须配置一些东西使它们以某种方式过期,但如果没有您的代码,我无法说明这可能是什么。@TonyPacheco我添加了cookie Auth events处理程序和启动设置,那么您得到赏金了吗?XDIt似乎没有:)
services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddCookie(options =>
            {
                options.ExpireTimeSpan = TimeSpan.FromHours(1);
                options.Cookie.SameSite = SameSiteMode.Lax;
                options.Cookie.Name = "wtf";
                options.EventsType = typeof(CookieAuth);
                options.SlidingExpiration = true;
            });