private ClaimsIdentity GetClaimsFromAuthModel(AcordaControlOffline.Shared.ApplicationServices.ViewModel.Auth authModel) { var claims = new List <Claim> { new Claim(ClaimTypes.Name, authModel.Username), new Claim(ClaimTypes.Role, authModel.Role), new Claim(ClaimTypes.StateOrProvince, authModel.CantonCode), }; var claimsIdentity = new ClaimsIdentity(claims, "basicAuth"); return(claimsIdentity); }
public async Task Login(LoginModel loginModel) { string basicAuthToken = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{loginModel.CantonCode}.{loginModel.Username}:{loginModel.Password}")); string role = "inspector"; // todo get role from api POST to /login var auth = new AcordaControlOffline.Shared.ApplicationServices.ViewModel.Auth(loginModel.Username, role, loginModel.CantonCode, basicAuthToken); var settings = await settingsService_.Read(); await jsRuntime_.InvokeAsync <string>(JsInterop.SetCookie, AcordaControlOffline.Shared.ApplicationServices.ViewModel.Auth.CookieName, JsonConvert.SerializeObject(auth), settings.AuthCookieExpiryDays); httpClient_.SetBasicAuthToken(auth.Token); ((ApiAuthenticationStateProvider)authenticationStateProvider_).MarkUserAsAuthenticated(auth); }
public void MarkUserAsAuthenticated(AcordaControlOffline.Shared.ApplicationServices.ViewModel.Auth authModel) { var authenticated = new ClaimsPrincipal(GetClaimsFromAuthModel(authModel)); NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(authenticated))); }