Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.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/0/asp.net-core/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服务器端应用程序中扩展身份验证过程?_C#_Asp.net Core_Blazor_Blazor Server Side - Fatal编程技术网

C# 如何在blazor服务器端应用程序中扩展身份验证过程?

C# 如何在blazor服务器端应用程序中扩展身份验证过程?,c#,asp.net-core,blazor,blazor-server-side,C#,Asp.net Core,Blazor,Blazor Server Side,我正在blazor服务器端应用程序中使用Cookie身份验证 await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); 我现在要做的是,每当blazor服务器端应用程序验证用户授权时,更改静态类中的静态变量。有什么简单的方法可以做到这一点吗 编辑: 下面是基于Plamen Yordan

我正在blazor服务器端应用程序中使用Cookie身份验证

await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties);
我现在要做的是,每当blazor服务器端应用程序验证用户授权时,更改静态类中的静态变量。有什么简单的方法可以做到这一点吗

编辑: 下面是基于Plamen Yordanov解决方案的实现

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, configureOptions =>
                {
                    bool personalNummerConfigured = false;
                    configureOptions.Events.OnValidatePrincipal = (context) =>
                    {
                        if (!personalNummerConfigured)
                        {
                            var employeeClaim = context.Principal.Claims.Where(x => x.Type == "EmployeeId").FirstOrDefault();
                            int.TryParse(employeeClaim.Value, out int personalNummer);
                            FbController.PersonalNummer = personalNummer;
                            Console.WriteLine($"PersonalNummer: {employeeClaim.Value}");
                            personalNummerConfigured = true;
                        }
                        return Task.CompletedTask;
                    };
                });

您可以订阅某些cookie身份验证事件,并且其中有一个OnSignedIn

可以在
Startup.cs
中配置此事件,如下所示:

.AddCookie("AppCookie", configureOptions => {
     configureOptions.Events.OnSignedIn = (context) => 
         {
             //handler code
             return Task.CompletedTask;
         };
 });

您可以在官方

Avoid
static
中尽可能多地查看您可以订阅的所有活动。特别是授权,谢谢!这正是我所寻找的。但是我必须使用OnValidatePrincipalEvent,因为当有人重新访问该站点时,会调用这个。我将我的答案添加到上面的问题中。