예제 #1
0
 public static async Task <string> GenerateAccessToken(
     IAccountsService accountsService,
     string userName,
     string ipAddress         = default,
     DateTime?creationDate    = default,
     string issuer            = default,
     string audience          = default,
     string signingKey        = default,
     double?durationInMinutes = default)
 {
     try
     {
         return((await accountsService.GenerateAccessTokenAsync(
                     userName: userName,
                     ipAddress: ipAddress ?? "127.0.0.1",
                     creationDate: creationDate ?? DateTime.UtcNow,
                     issuer: issuer,
                     audience: audience,
                     signingKey: signingKey,
                     durationInMinutes: durationInMinutes)).AccessToken);
     }
     catch (Exception ex)
     {
         throw new Exception($"Exception thrown on the test helper method 'GenerateAccessToken' with the message: {ex.Message}");
     }
 }
예제 #2
0
        public static async Task Authenticate(
            this HttpClient client,
            IAccountsService accountsService,
            string userName          = default,
            string accessToken       = default,
            string ipAddress         = default,
            DateTime?creationDate    = default,
            string issuer            = default,
            string audience          = default,
            string signingKey        = default,
            double?durationInMinutes = default)
        {
            if (accessToken == default)
            {
                try
                {
                    accessToken = (await accountsService.GenerateAccessTokenAsync(
                                       userName: userName,
                                       ipAddress: ipAddress ?? "127.0.0.1",
                                       creationDate: creationDate ?? DateTime.UtcNow,
                                       issuer: issuer,
                                       audience: audience,
                                       signingKey: signingKey,
                                       durationInMinutes: durationInMinutes)).AccessToken;
                }
                catch (Exception ex)
                {
                    throw new Exception($"Exception thrown on the test helper method 'Authenticate' with the message: {ex.Message}");
                }
            }

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
        }