public async Task RefreshTokenAsync() { try { var data = _storageService.LoadSignInInfo(); Data = data; return; if (data == null || string.IsNullOrWhiteSpace(data.RefreshToken)) { return; } var client = new OidcClient(GetIdentityOptions()); var result = await client.GetUserInfoAsync(data.AccessToken); if (result.IsError) { return; } var displayNameClaim = result.Claims?.FirstOrDefault(t => t.Type == Resources.DisplayNameClaimType); if (displayNameClaim != null) { data.UserDisplayName = displayNameClaim.Value; } Data = _storageService.SaveSignInInfo(data); } catch (Exception ex) { var message = ex.ToString(); throw; } }
public SigninStore SaveSignInInfo(SigninStore obj) { var filePath = getFilePath(obj.GetType()); var data = JsonConvert.SerializeObject(obj); lock (_lockObject) { File.WriteAllText(filePath, data); } return(obj); }
public async Task <SignInResult> SignInAsync() { ResetInfo(); var client = new OidcClient(GetIdentityOptions()); var result = new SignInResult(await client.LoginAsync(new LoginRequest { BrowserDisplayMode = DisplayMode.Visible })); if (result.IsError) { //MessageBox.Show(this, result.Error, "Login", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { var info = new SigninStore { AccessToken = result.AccessToken, RefreshToken = result.RefreshToken, UserDisplayName = result.User?.Identity?.Name, //RefreshHandler = result.RefreshTokenHandler }; if (string.IsNullOrWhiteSpace(info.UserDisplayName)) { var displayNameClaim = result.User?.Claims?.FirstOrDefault(t => t.Type == Resources.DisplayNameClaimType); if (displayNameClaim != null) { info.UserDisplayName = displayNameClaim.Value; } } //var sb = new StringBuilder(128); //foreach (var claim in result.User.Claims) //{ // sb.AppendLine($"{claim.Type}: {claim.Value}"); //} //if (!string.IsNullOrWhiteSpace(result.RefreshToken)) //{ // sb.AppendLine($"refresh token: {result.RefreshToken}"); //} Data = _storageService.SaveSignInInfo(info); //_apiClient = new HttpClient(result.RefreshTokenHandler); //_apiClient.BaseAddress = new Uri("https://demo.identityserver.io/api/"); } return(result); }
public SigninStore LoadSignInInfo() { var filePath = getFilePath(typeof(SigninStore)); SigninStore data = new SigninStore(); if (!File.Exists(filePath)) { return(data); } lock (_lockObject) { var file = File.ReadAllText(filePath); data = JsonConvert.DeserializeObject <SigninStore>(file); } return(data); }
private void ResetInfo() { Data = _storageService.SaveSignInInfo(new SigninStore()); }