private static async Task <string> RegisterPasswordClientAndGetAccessToken(TestAccount user, string secret, ServerTester tester) { var id = Guid.NewGuid().ToString(); var openIdClient = await user.RegisterOpenIdClient( new OpenIddictApplicationDescriptor() { ClientId = id, DisplayName = id, Permissions = { OpenIddictConstants.Permissions.GrantTypes.Password } }, secret); var httpClient = tester.PayTester.HttpClient; var httpRequest = new HttpRequestMessage(HttpMethod.Post, new Uri(tester.PayTester.ServerUri, "/connect/token")) { Content = new FormUrlEncodedContent(new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("grant_type", OpenIddictConstants.GrantTypes.Password), new KeyValuePair <string, string>("username", user.RegisterDetails.Email), new KeyValuePair <string, string>("password", user.RegisterDetails.Password), new KeyValuePair <string, string>("client_id", openIdClient.ClientId), new KeyValuePair <string, string>("client_secret", secret), new KeyValuePair <string, string>("scope", "server_management store_management") }) }; var response = await httpClient.SendAsync(httpRequest); Assert.True(response.IsSuccessStatusCode); string content = await response.Content.ReadAsStringAsync(); var result = System.Text.Json.JsonSerializer.Deserialize <OpenIddictResponse>(content); Assert.NotEmpty(result.AccessToken); Assert.Null(result.Error); return(result.AccessToken); }
private static async Task <string> RegisterClientCredentialsFlowAndGetAccessToken(TestAccount user, string secret, ServerTester tester) { var id = Guid.NewGuid().ToString(); var openIdClient = await user.RegisterOpenIdClient( new OpenIddictApplicationDescriptor() { ClientId = id, DisplayName = id, Permissions = { OpenIddictConstants.Permissions.GrantTypes.ClientCredentials } }, secret); var httpClient = tester.PayTester.HttpClient; var httpRequest = new HttpRequestMessage(HttpMethod.Post, new Uri(tester.PayTester.ServerUri, "/connect/token")) { Content = new FormUrlEncodedContent(new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("grant_type", OpenIddictConstants.GrantTypes.ClientCredentials), new KeyValuePair <string, string>("client_id", openIdClient.ClientId), new KeyValuePair <string, string>("client_secret", secret) }) }; var response = await httpClient.SendAsync(httpRequest); Assert.True(response.IsSuccessStatusCode); string content = await response.Content.ReadAsStringAsync(); var result = JObject.Parse(content).ToObject <OpenIdConnectResponse>(); Assert.NotEmpty(result.AccessToken); Assert.Null(result.Error); return(result.AccessToken); }