title: WebAPI Non-interactive authenticating to Azure AD using username & password
body: <p>I have a Rest service hosted on Azure with Azure AD Authentication enabled on it.  I am consuming this Web service in Microsoft Add in.</p>
  
  <p>I do not want to prompt the Microsoft login page to user. Instead login in back ground using his user name and password and get access token and refresh token.</p>
  
  <p>I have been using below code : </p>
  
  <pre><code> private static string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
   AuthenticationResult result = null;
   string user = "";
   string password = "";
   string resrouce = "web service URL";
   authContext = new AuthenticationContext(authority, new FileCache());
  
   UserCredential uc = new UserPasswordCredential(user, password);
   result = authContext.AcquireTokenAsync(todoListResourceId, clientId, uc).Result;
  </code></pre>
  
  <p>The exception for this code is “The request body must contain the following parameter: client_secret or client_assertion” . I did not find a way to pass client secret or client assertion with User credentials. </p>
  
  <p>I have also tried below code.  In below code using Client ID and Client Secret key and getting back an access token that will expire in an hour and this code does not return refresh token. This access token is not specific to the user.</p>
  
  <pre><code> string tenantName = "contoso.com";
   string authString = "https://login.microsoftonline.com/" + tenantName;
   AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
  string clientId = "a3b4eef4-33f0-4e98-9d57-ad14549bf310";
  string key = "Secret Key";
  ClientCredential clientCred = new ClientCredential(clientId, key);
  string resource = "Service URL ";
  string token;
  AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(resource, clientCred).Result;
  token = authenticationResult.AccessToken;
  </code></pre>
  
  <p>is there any way to get the access token and refresh token from Azure AD without prompting user Microsoft login form.</p>