/// <summary>
        /// GitHubToAadConverter constructor for generating new token, and initialize http client.
        /// </summary>
        /// <param name="credential">The aad token auth class.</param>
        /// <param name="logger">Logger</param>
        public GitHubToAADConverter(
            ClientSecretCredential credential,
            ILogger <GitHubToAADConverter> logger)
        {
            this.logger = logger;
            var opsAuthToken = "";

            try
            {
                // This is aad scope of opensource rest API.
                string[] scopes = new string[]
                {
                    "api://5bc5e692-fe67-4053-8d49-9e2863718bfb/.default"
                };
                opsAuthToken = credential.GetToken(new TokenRequestContext(scopes)).Token;
            }
            catch (Exception ex)
            {
                logger.LogError("Failed to generate aad token. " + ex.Message);
            }
            client = new HttpClient();
            client.DefaultRequestHeaders.Add("content_type", "application/json");
            client.DefaultRequestHeaders.Add("api-version", "2019-10-01");
            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {opsAuthToken}");
        }
示例#2
0
        public static string GetAccessToken(this IConfigurationRoot config)
        {
            var credential  = new ClientSecretCredential(config["AuthTenantId"], config["AuthClientId"], config["AuthClientSecret"]);
            var accessToken = credential.GetToken(new TokenRequestContext(new[] { config["AuthScope"] }));

            return(accessToken.Token);
        }
示例#3
0
        public static IDbConnection CreateConnection(SquealConfig config)
        {
            string accessToken = null;

            if (!String.IsNullOrEmpty(config.AzureAdAuthentication.TenantId) &&
                !String.IsNullOrEmpty(config.AzureAdAuthentication.ClientId))
            {
                var credential = new ClientSecretCredential(config.AzureAdAuthentication.TenantId,
                                                            config.AzureAdAuthentication.ClientId, config.AzureAdAuthentication.ClientSecret);

                var token = credential.GetToken(new Azure.Core.TokenRequestContext(
                                                    new string[] { "https://database.windows.net/.default" }));

                accessToken = token.Token;
            }

            var connection = new SqlConnection(config.ConnectionString);

            connection.AccessToken = accessToken;
            return(connection);
        }
示例#4
0
        /// <summary>
        /// Will create a system topic on a storage account where Azure will publish events for containers in the storage account.
        /// Only one system topic can be created per storage account.
        /// </summary>
        private bool CreateStorageSystemTopic(string _TopicName, Action <string> _ErrorMessageAction = null)
        {
            try
            {
                SystemTopic TopicInfo = new SystemTopic(ResourceGroupLocation,
                                                        $"/subscriptions/{SubscriptionId}/resourceGroups/{ResourceGroupName}/providers/Microsoft.EventGrid/systemTopics/{_TopicName}",
                                                        _TopicName,
                                                        "Microsoft.EventGrid/systemTopics",
                                                        null,
                                                        null,
                                                        $"/subscriptions/{SubscriptionId}/resourceGroups/{ResourceGroupName}/providers/microsoft.storage/storageaccounts/{StorageAccountName}",
                                                        null,
                                                        "microsoft.storage.storageaccounts");

                if (LastGeneratedToken.ExpiresOn.UtcDateTime <= DateTime.UtcNow)
                {
                    ClientSecretCredential ClientCred     = new ClientSecretCredential(TenantId, ClientId, ClientSecret);
                    TokenRequestContext    RequestContext = new TokenRequestContext(new string[] { $"https://management.azure.com/.default" });
                    LastGeneratedToken = ClientCred.GetToken(RequestContext);
                }

                TokenCredentials TokenCredential = new TokenCredentials(new StringTokenProvider(LastGeneratedToken.Token, "Bearer"));

                EventGridManagementClient ManagmentClient = new EventGridManagementClient(TokenCredential);
                TokenCredential.InitializeServiceClient(ManagmentClient);
                ManagmentClient.SubscriptionId = SubscriptionId;

                AZSystemTopicOperations SystemTopicOperations = new AZSystemTopicOperations(ManagmentClient);

                bool Success = SystemTopicOperations.CreateOrUpdate(ResourceGroupName, _TopicName, TopicInfo, out SystemTopic _, _ErrorMessageAction);

                return(Success);
            }
            catch (Exception ex)
            {
                _ErrorMessageAction?.Invoke($"{ex.Message} :\n {ex.StackTrace}");
                return(false);
            }
        }