using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using Microsoft.IdentityModel.Tokens; var claims = new[] { new Claim(JwtRegisteredClaimNames.Sub, "username"), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("supersecretkey")); var token = new JwtSecurityToken( "issuer", "audience", claims, expires: DateTime.Now.AddMinutes(30), signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256)); var jwtToken = new JwtSecurityTokenHandler().WriteToken(token);
using Microsoft.IdentityModel.Clients.ActiveDirectory; using System.Threading.Tasks; var authContext = new AuthenticationContext("https://login.microsoftonline.com/common"); var authResult = await authContext.AcquireTokenAsync( "https://resource.url", "clientid", new Uri("https://redirect.uri"), new PlatformParameters(PromptBehavior.Auto)); var accessToken = authResult.AccessToken;This code creates a new AuthenticationContext object, specifying the URL of the authentication server. It then uses that context to acquire a token, providing the resource URL, client ID, and redirect URI. It prompts the user to authenticate if necessary, then returns an access token.