public static async Task LoginResourceOwner(string username, string userpassword) { DiscoveryClient discoClient = new DiscoveryClient(ApiServerURL); discoClient.Policy.RequireHttps = false; DiscoveryResponse disco = await discoClient.GetAsync(); if (disco.IsError) { throw new Exception(disco.Error); } TokenClient client = new TokenClient(disco.TokenEndpoint, ClientName, ClientSecret); TokenResponse result = await client.RequestResourceOwnerPasswordAsync(username, userpassword, string.Join(" ", ClientScopes)); if (result.IsError) { throw new Exception("Could not log in, username or password is incorrect, or you have not yet confirmed your email."); } StringBuilder sb = new StringBuilder(128); sb.AppendLine($"refresh token: {result.RefreshToken}"); sb.AppendLine($"access token: {result.AccessToken}"); System.Diagnostics.Debug.WriteLine(sb.ToString()); SessionSingleton.SetTokens(result.AccessToken, result.RefreshToken); }
public static async Task RequestRefreshToken() { DiscoveryClient discoClient = new DiscoveryClient(ApiServerURL); DiscoveryResponse disco = await discoClient.GetAsync(); if (disco.IsError) { throw new Exception(disco.Error); } TokenClient client = new TokenClient(disco.TokenEndpoint, ClientName, ClientSecret); TokenResponse result = await client.RequestRefreshTokenAsync(SessionSingleton.RefreshToken); if (result.IsError) { throw new Exception("Could not refresh token, try logoff and login again."); } SessionSingleton.SetTokens(result.AccessToken, result.RefreshToken); }