コード例 #1
0
        public override async Task <AuthenticationState> GetAuthenticationStateAsync()
        {
            var token = await _jsRuntime.GetCookieAsync(_authTokenKey);

            if (string.IsNullOrWhiteSpace(token))
            {
                await NavigateToLoginPageOnSecurityApp();
            }

            else
            {
                // Call Security client to confirm token
                var securityResponse = await _securityClientService.GetUserByTokenAsync(token, default);

                // No valid response or token has been expired
                if (securityResponse == null || !securityResponse.IsSuccessful)
                {
                    // todo: Log the response Message into Logger
                    await NavigateToLoginPageOnSecurityApp();
                }

                else //Valid token
                {
                    // Add token to all host http calls
                    _hostClient.DefaultRequestHeaders.Add(_authTokenKey, token);
                    _hostClient.DefaultRequestHeaders.Add("client-name", AppStrings.WebApp.ClientName);

                    // Create claims
                    var claims = new List <Claim>
                    {
                        new Claim(ClaimTypes.Name, $"{securityResponse.Data!.FName} {securityResponse.Data!.LName}"),
                        new Claim(ClaimTypes.NameIdentifier, securityResponse.Data !.UserID.ToString()),
                        new Claim(AppStrings.Claims.PersonnelCode, securityResponse.Data !.InfperCode.ToString()),
                        new Claim(AppStrings.Claims.Username, securityResponse.Data !.Username !)
                    };

                    // Add Roles to claims
                    var userRoles = await _securityClientService.GetUserRolesAsync(token, CancellationToken.None);

                    if (userRoles != null)
                    {
                        foreach (var role in userRoles)
                        {
                            if (role.Tag == null)
                            {
                                throw new DomainLogicException($"Role should have tag. Details: {role.SerializeToJson()}");
                            }

                            //if (role.IsAdmin)
                            //{
                            //    if (!claims.Any(c => c.Type == ClaimTypes.Role && c.Value == AppStrings.Claims.Administrator))
                            //        claims.Add(new Claim(ClaimTypes.Role, AppStrings.Claims.Administrator));
                            //}

                            if (!claims.Any(c => c.Type == ClaimTypes.Role && c.Value == role.Tag))
                            {
                                claims.Add(new Claim(ClaimTypes.Role, role.Tag));
                            }
                        }
                    }

                    // Creates ClaimsIdentity
                    var claimsIdentity = new ClaimsIdentity(claims, "ATASecurityAuthType");

                    // Creates ClaimsPrinciple
                    var claimsPrinciple = new ClaimsPrincipal(claimsIdentity);

                    return(new AuthenticationState(claimsPrinciple));
                }
            }

            // Anonymous
            return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(new List <Claim>()))));
        }