本文整理汇总了C#中IdentityModel.Client.TokenClient.RequestClientCredentialsAsync方法的典型用法代码示例。如果您正苦于以下问题:C# TokenClient.RequestClientCredentialsAsync方法的具体用法?C# TokenClient.RequestClientCredentialsAsync怎么用?C# TokenClient.RequestClientCredentialsAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IdentityModel.Client.TokenClient
的用法示例。
在下文中一共展示了TokenClient.RequestClientCredentialsAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
var tokenClient = new TokenClient(
"http://localhost:18942/connect/token",
"test",
"secret");
//This responds with the token for the "api" scope, based on the username/password above
var response = tokenClient.RequestClientCredentialsAsync("api1").Result;
//Test area to show api/values is protected
//Should return that the request is unauthorized
try
{
var unTokenedClient = new HttpClient();
var unTokenedClientResponse = unTokenedClient.GetAsync("http://localhost:19806/api/values").Result;
Console.WriteLine("Un-tokened response: {0}", unTokenedClientResponse.StatusCode);
}
catch (Exception ex)
{
Console.WriteLine("Exception of: {0} while calling api without token.", ex.Message);
}
//Now we make the same request with the token received by the auth service.
var client = new HttpClient();
client.SetBearerToken(response.AccessToken);
var apiResponse = client.GetAsync("http://localhost:19806/identity").Result;
var callApiResponse = client.GetAsync("http://localhost:19806/api/values").Result;
Console.WriteLine("Tokened response: {0}", callApiResponse.StatusCode);
Console.WriteLine(callApiResponse.Content.ReadAsStringAsync().Result);
Console.Read();
}
示例2: Valid_Client_Multiple_Scopes
public async Task Valid_Client_Multiple_Scopes()
{
var client = new TokenClient(
TokenEndpoint,
"client",
"secret",
innerHttpMessageHandler: _handler);
var response = await client.RequestClientCredentialsAsync("api1 api2");
response.IsError.Should().Be(false);
response.ExpiresIn.Should().Be(3600);
response.TokenType.Should().Be("Bearer");
response.IdentityToken.Should().BeNull();
response.RefreshToken.Should().BeNull();
var payload = GetPayload(response);
payload.Count().Should().Be(6);
payload.Should().Contain("iss", "https://idsrv4");
payload.Should().Contain("aud", "https://idsrv4/resources");
payload.Should().Contain("client_id", "client");
var scopes = payload["scope"] as JArray;
scopes.Count().Should().Be(2);
scopes.First().ToString().Should().Be("api1");
scopes.Skip(1).First().ToString().Should().Be("api2");
}
示例3: RequestAccessTokenClientCredentials
private static string RequestAccessTokenClientCredentials()
{
//did we store the token before?
var cookie = HttpContext.Current.Request.Cookies.Get("tripGalleryCookie");
if (cookie != null && cookie["access_token"] != null)
{
return cookie["access_token"];
}
var tokenClient = new TokenClient(
TripGallery.Constants.TripGallerySTSTokenEndpoint,
"tripgalleryclientcredentials",
TripGallery.Constants.TripGalleryClientSecret
);
var tokenResponse = tokenClient.RequestClientCredentialsAsync("gallerymanagement").Result;
//just to debug
TokenHelper.DecodeAndWrite(tokenResponse.AccessToken);
//save token in a cookie
HttpContext.Current.Response.Cookies["TripGalleryCookie"]["access_token"] = tokenResponse.AccessToken;
return tokenResponse.AccessToken;
}
示例4: GetTokenAsync
/// <summary>
/// Busca o token de acesso no IdentityServer para ser utilizado na API.
/// </summary>
/// <returns></returns>
private async Task<TokenResponse> GetTokenAsync()
{
var client = new TokenClient("https://localhost:44302/identity/connect/token"
, "mvc_service"
, "secret");
return await client.RequestClientCredentialsAsync("gac_erp_appservice");
}
示例5: RequestAccessTokenClientCredentials
/// <summary>
/// request ID server for a token using the ClientId and secret
/// </summary>
/// <returns>An access token in string format</returns>
private static string RequestAccessTokenClientCredentials(string clientId,string secret)
{
// did we store the token before?
var cookie = HttpContext.Current.Request.Cookies.Get("TripGalleryCookie");
if (cookie != null && cookie["access_token"] != null)
{
return cookie["access_token"];
}
// no token found - get one
// create an oAuth2 Client
var oAuth2Client = new TokenClient(
TripGallery.Constants.TripGallerySTSTokenEndpoint,
clientId,
secret);
// ask for a token, containing the gallerymanagement scope
var tokenResponse = oAuth2Client.RequestClientCredentialsAsync("gallerymanagement").Result;
// decode & write out the token, so we can see what's in it
// TokenHelper.DecodeAndWrite(tokenResponse.AccessToken);
// we save the token in a cookie for use later on
HttpContext.Current.Response.Cookies["TripGalleryCookie"]["access_token"] = tokenResponse.AccessToken;
// return the token
return tokenResponse.AccessToken;
}
示例6: GetToken
static TokenResponse GetToken(string tokenUrl, string clientId, string secret, string scope, string username = null, string password = null)
{
var client = new TokenClient(tokenUrl, clientId, secret);
if (string.IsNullOrWhiteSpace(username)||string.IsNullOrWhiteSpace(password))
return client.RequestClientCredentialsAsync(scope).Result;
else
return client.RequestResourceOwnerPasswordAsync(username, password, scope).Result;
}
示例7: GetClientToken
static TokenResponse GetClientToken()
{
var client = new TokenClient("https://localhost:44333/connect/token",
"freightshare1",
"IIPiBTywUcK5Qv0kvmVXbSiax5wBStDMGTAIA0T/RSM=");
return client.RequestClientCredentialsAsync("api1").Result;
}
示例8: GetClientToken
static TokenResponse GetClientToken()
{
var client = new TokenClient(
"https://100.105.80.38:13855/connect/token",
"silicon",
"F621F470-9731-4A25-80EF-67A6F7C5F4B8");
return client.RequestClientCredentialsAsync("api1").Result;
}
示例9: RequestToken
private async static Task<string> RequestToken()
{
var tokenClient = new TokenClient("https://localhost:44302/connect/token", "ConsoleClient", "secret");
var response = await tokenClient.RequestClientCredentialsAsync("Api");
return response.AccessToken;
}
示例10: GetTokenAsync
private async Task<TokenResponse> GetTokenAsync()
{
var client = new TokenClient(IdConstants.IdHost +
"identity/connect/token",
"mvc_service",
"secret");
return await client.RequestClientCredentialsAsync("sampleApi");
}
示例11: GetToken
static TokenResponse GetToken()
{
var client = new TokenClient(
"https://localhost:44300/connect/token",
"test",
"secret");
return client.RequestClientCredentialsAsync("api1").Result;
}
示例12: GetClientToken
//requests the access token using the client credentials
static TokenResponse GetClientToken()
{
var client = new TokenClient(
"http://localhost:44333/connect/token",
"silicon",
"F621F470-9731-4A25-80EF-67A6F7C5F4B8");
return client.RequestClientCredentialsAsync("api1").Result;
}
示例13: RequestToken
static TokenResponse RequestToken()
{
var client = new TokenClient(
Constants.TokenEndpoint,
"client",
"secret");
return client.RequestClientCredentialsAsync("read write").Result;
}
示例14: GetTokenAsync
protected async Task<TokenResponse> GetTokenAsync()
{
string _url = Constants.IdentityServerUri + "/connect/token";
var client = new TokenClient(
_url,
Constants.APIClient,
Constants.IdentitySecret);
return await client.RequestClientCredentialsAsync("apiAccess");
}
示例15: RequestToken
static TokenResponse RequestToken()
{
var client = new TokenClient(
Constants.TokenEndpoint,
"clientcredentials.client",
"secret",
AuthenticationStyle.PostValues);
return client.RequestClientCredentialsAsync("read write").Result;
}