/// <summary> /// Constructor to create credential with client id and secret. This is only available on desktop. /// </summary> /// <param name="clientId">Identifier of the client requesting the token.</param> /// <param name="secureClientSecret">Secure secret of the client requesting the token.</param> public ClientCredential(string clientId, ISecureClientSecret secureClientSecret) { if (string.IsNullOrWhiteSpace(clientId)) { throw new ArgumentNullException("clientId"); } if (secureClientSecret == null) { throw new ArgumentNullException("clientSecret"); } this.ClientId = clientId; this.SecureClientSecret = secureClientSecret; }
// Authenticate the application with AAD through the application's secret key. // You need to have an application registered with AAD in order to authenticate. // For more information and instructions on how to register your application with AAD, see: // https://azure.microsoft.com/en-us/documentation/articles/resource-group-create-service-principal-portal/ public static TokenCredentials AuthenticateApplication(string tenantId, string resource, string appClientId, Uri appRedirectUri, ISecureClientSecret clientSecret) { var authContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId); var credential = new ClientCredential(appClientId, clientSecret); var tokenAuthResult = authContext.AcquireTokenAsync(resource, credential).Result; return(new TokenCredentials(tokenAuthResult.AccessToken)); }