Token Generation : Utility method for generating a token on call.
/* Token Generator Method
• This Method is paired with a POSTMAN service endpoint that will return a token.
• Preinitialized parameters for username and password are used.
• Client object created (REF: HttpClient object)
• Path set : Specifies the endpoint to which this pertains to
• Form : A string formatting action to correctly encode via use of HttpUtility class
• Response -- A post action through the client object, passing in the path (eendpoint)
• Content is stored from the response with the ReadAsStringAsync() method.
• Result is the content object deserialized. Dynamic keyword used
• Result.access_token if returned.
*/
public static async Task<string> GetToken(string username = "jakearmitage@example.com", string password = "shadowrun") {
var client = BuildConsumerServiceClient();
var path = "api/token";
var form = String.Format("grant_type=password&username={0}&password={1}", HttpUtility.UrlEncode(username), HttpUtility.UrlEncode(password));
var response = await client.PostAsync(path, new StringContent(form));
var content = await response.Content.ReadAsStringAsync();
dynamic result = JsonConvert.DeserializeObject(content);
return result.access_token;
}
// Used Elsewhere in Code
// To generated a token for use
[Fact]
public async Task ServiceShouldAllowPasswordResets() {
var token = await UtilityHelpers.GetToken();
var client = UtilityHelpers.BuildConsumerServiceClient(token);
var path = "api/Account/UpdatePassword";
var oldPassword = "cyberpunk";