title: How to set custom claims to aad token using C# code
body: <p>I have a webapi which generates <code>aad</code> token and I have written token generation logic in <code>Get()</code> method in webapi.</p>
  
  <p>I'm able generate aad jwt token from webapi <code>get()</code> method but, now I want to include some custom claims into the token. </p>
  
  <p>How can I set custom claims to aad token using c#.</p>
  
  <p>I have used below code for generating aad token.</p>
  
  <pre><code>var authenticationContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext("https://login.windows.net/" + ConfigurationManager.AppSettings["TenantID"].ToString());
              var credential = new ClientCredential(clientId: ConfigurationManager.AppSettings["ClientID"].ToString(), clientSecret: secret);
              var result = await authenticationContext.AcquireTokenAsync(
                  ConfigurationManager.AppSettings["Resource"].ToString(),
                  credential
                  ).ConfigureAwait(false);
  </code></pre>
  
  <p>Kindly share any sample  c# code to set custom claims to aad token generated from above code .</p>
  
  <p><strong>Note:</strong> I want to set a new custom claim for aad token where custom claim value obtained from external logic.</p>
  
  <p><strong>Update 1:</strong></p>
  
  <p>Looks like below post may be useful.</p>
  
  <p><a href="https://www.rahulpnath.com/blog/azure-ad-custom-attributes-and-optional-claims-from-an-asp-dot-net-application/" rel="nofollow noreferrer">https://www.rahulpnath.com/blog/azure-ad-custom-attributes-and-optional-claims-from-an-asp-dot-net-application/</a></p>
  
  <p>I tried below following above post.</p>
  
  <p>Generated jwt token to call Graph API. But I got blocked at below code.</p>
  
  <pre><code>    var dictionary = new Dictionary&lt;string, object&gt;();
          dictionary.Add(employeeCodePropertyName, employee.Code);
  
  //Here I can't use graphApiClient.Users because, I don't have any user info on my jwt token. It will be just Access token which as details related to aad application.I want to update extension attribute which is present in OptionalClaims -&gt; Access Token of AAD Application Manifest.
          await graphApiClient.Users[employee.EmailAddress]  
              .Request()
              .UpdateAsync(new User()
              {
                  AdditionalData = dictionary
              });
  </code></pre>
  
  <p>How to update extension claim attribute present in access token of optional claims . I want to update through c# code.
  How to do that. Kindly suggest.</p>
  
  <p><strong>Update 2:</strong></p>
  
  <p>I want to use code similar to below for updating custom extension claim attribute present in optional claims of azure ad app but <code>UpdateAsync</code> is not working.</p>
  
  <pre><code>await graphApiClient.Application[clientid]  
              .Request()
              .UpdateAsync(new Application()
              {
                  AdditionalData = dictionary
              });
  </code></pre>
  
  <p>At <code>UpdateAsync()</code>, I'm getting issue as below</p>
  
  <pre><code>Specified HTTP method is not allowed for the request
  </code></pre>
  
  <p>Kindly let me know the solution to add custom user defined claim to azure ad access token. </p>
  
  <p><strong>Note:</strong> Since, I want to update access token ,there will be not be any user info on access token. So, <code>graphApiClient.Users[employee.EmailAddress]</code>  won't work, as access token will not have any user info like <code>EmailAddress</code></p>
  
  <p>I have created extension claim attribute in azure ad ap manifest as below</p>
  
  <p><a href="https://i.stack.imgur.com/8yXC7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8yXC7.png" alt="enter image description here"></a></p>
  
  <p>I want to update extension claim attribute <code>extension_clientid_moviename</code> value dynamically with value obtained from external source through code. How to do that. Kindly suggest.</p>
  
  <p><strong>Update 3:</strong></p>
  
  <p>I have tried below code to update extension claim attribute present in Optional claims ID Token.</p>
  
  <pre><code>   await graphApiServiceClient.Users["abcd@hotmail.com"]
                  .Request()
                  .UpdateAsync(new User()
                  {
                      AdditionalData = dictionary
                  });
  </code></pre>
  
  <p>I am getting error as below</p>
  
  <pre><code>Code: Request_ResourceNotFound\r\nMessage: Resource '' does not exist or one of its queried reference-property objects are not present.
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/IQGKo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IQGKo.png" alt="enter image description here"></a></p>