Created
May 17, 2021 13:01
-
-
Save Platonenkov/faabfd7f21b541ee678240bf0cd39c48 to your computer and use it in GitHub Desktop.
Auth clients
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static async Task<string> CallServiceAsync(string token, string controller) | |
{ | |
var baseAddress = Constants.SampleApi; | |
var client = new HttpClient | |
{ | |
BaseAddress = new Uri(baseAddress) | |
}; | |
client.SetBearerToken(token); | |
try | |
{ | |
var response = await client.GetStringAsync(controller); | |
return response; | |
} | |
catch (Exception e) | |
{ | |
return e.Message; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using IdentityModel.Client; | |
// discover endpoints from metadata | |
var client = new HttpClient(); | |
var disco = await client.GetDiscoveryDocumentAsync(Constants.Authority); | |
if (disco.IsError) | |
{ | |
Console.WriteLine(disco.Error); | |
return; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// request token | |
var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest | |
{ | |
Address = disco.TokenEndpoint, | |
ClientId = "Api1_client_id", | |
ClientSecret = "secret", | |
Scope = "api1" | |
}); | |
// call api | |
var apiClient = new HttpClient(){ BaseAddress = new Uri(api-uri) }; | |
apiClient.SetBearerToken(tokenResponse.AccessToken); | |
var response = await apiClient.GetAsync(api-controller/method); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Get user claims from server | |
public static async Task<Dictionary<string,string>> GetUserInfoAsync(string accessToken) | |
{ | |
var client = new HttpClient(); | |
var disco = await client.GetDiscoveryDocumentAsync(Constants.Authority); | |
if (disco.IsError) throw new Exception(disco.Error); | |
var user_data = await client.GetUserInfoAsync( | |
new UserInfoRequest() { Address = disco.UserInfoEndpoint, Token = accessToken }); | |
if (!user_data.IsError) | |
return user_data.Claims.ToDictionary(claim => claim.Type, claim => claim.Value); | |
user_data.Error.ConsoleRed(); | |
return new Dictionary<string, string>(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Get tokens | |
static async Task<TokenResponse> RequestTokenAsync() | |
{ | |
var client = new HttpClient(); | |
var disco = await client.GetDiscoveryDocumentAsync(Constants.Authority); | |
if (disco.IsError) throw new Exception(disco.Error); | |
var token_request = new PasswordTokenRequest() | |
{ | |
Address = disco.TokenEndpoint, | |
//ClientSecret = "secret", | |
UserName = "admin", | |
Password = "qwe123", | |
ClientId = "console_login_client", | |
GrantType = "password", | |
Scope = "openid email profile api1 offline_access", | |
}; | |
var response = await client.RequestPasswordTokenAsync(token_request); | |
return response; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var options = new OidcClientOptions() | |
{ | |
Authority = Constants.Authority, | |
ClientId = "console_pkce", | |
RedirectUri = redirectUri, | |
PostLogoutRedirectUri = redirectUri, | |
Scope = "openid profile api1 offline_access email", | |
FilterClaims = false, | |
Browser = browser | |
}; | |
var oidcClient = new OidcClient(options); | |
var result = await oidcClient.LoginAsync(new LoginRequest()); | |
#region view result | |
if (result.IsError) | |
{ | |
Console.WriteLine("\n\nError:\n{0}", result.Error); | |
return; | |
} | |
Console.WriteLine("\n\nClaims:"); | |
foreach (var claim in result.User.Claims) | |
{ | |
Console.WriteLine("{0}: {1}", claim.Type, claim.Value); | |
} | |
Console.WriteLine($"\nidentity token: {result.IdentityToken}"); | |
Console.WriteLine($"access token: {result.AccessToken}"); | |
Console.WriteLine($"refresh token: {result?.RefreshToken ?? "none"}"); | |
#endregion | |
//LogOut | |
await _oidcClient.LogoutAsync(); | |
//Refresh Token | |
var refreshResult = await _oidcClient.RefreshTokenAsync(currentRefreshToken); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment