I'm facing an issue with CreateJwtSecurityToken from System.IdentityModel.Tokens.Jwt in my application. Strangely, the code works flawlessly on my local machine running Windows 11 Pro, but when deployed to a Windows Server 2022 VM, it encounters issues. The application relies on JWT token generation. Note: Application is hosted in IIS of Windows server 2022. Version for System.IdentityModel.Tokens.Jwt is 6.23.1.0.

public string CreateJsonWebToken(Token properties)
{
    try
    {
        log.Info(String.Concat("Message:", "Entered CreateJsonWebToken()", ";ActionMethod:", "CreateJsonWebToken", ";TimeStamp:", DateTime.Now.ToLongDateString()));

        var tokenHandler = new JwtSecurityTokenHandler();

        log.Info(String.Concat("Message:", "properties.issuer: " + properties.issuer, ";ActionMethod:", "CreateJsonWebToken", ";TimeStamp:", DateTime.Now.ToLongDateString()));
        log.Info(String.Concat("Message:", "properties.audience: " + properties.audience, ";ActionMethod:", "CreateJsonWebToken", ";TimeStamp:", DateTime.Now.ToLongDateString()));
        log.Info(String.Concat("Message:", "properties.expires: " + properties.expires, ";ActionMethod:", "CreateJsonWebToken", ";TimeStamp:", DateTime.Now.ToLongDateString()));
        log.Info(String.Concat("Message:", "properties.notBefore: " + properties.notBefore, ";ActionMethod:", "CreateJsonWebToken", ";TimeStamp:", DateTime.Now.ToLongDateString()));
        log.Info(String.Concat("Message:", "properties.signingCredentials: " + properties.signingCredentials, ";ActionMethod:", "CreateJsonWebToken", ";TimeStamp:", DateTime.Now.ToLongDateString()));
//Below lines are not executed
        var jwtToken = tokenHandler.CreateJwtSecurityToken(
            properties.issuer,
            properties.audience,
            properties.subject,
            properties.notBefore,
            properties.expires,
            DateTime.UtcNow,
            properties.signingCredentials);
        log.Info(String.Concat("Message:", "jwtToken -> header and claim: " + jwtToken, ";ActionMethod:", "CreateJsonWebToken", ";TimeStamp:", DateTime.Now.ToLongDateString()));
        log.Info(String.Concat("Message:", "jwtToken.EncodedHeader: " + jwtToken.EncodedHeader, ";ActionMethod:", "CreateJsonWebToken", ";TimeStamp:", DateTime.Now.ToLongDateString()));
        log.Info(String.Concat("Message:", "jwtToken.EncodedPayload: " + jwtToken.EncodedPayload, ";ActionMethod:", "CreateJsonWebToken", ";TimeStamp:", DateTime.Now.ToLongDateString()));
        log.Info(String.Concat("Message:", "jwtToken.IssuedAt: " + jwtToken.IssuedAt, ";ActionMethod:", "CreateJsonWebToken", ";TimeStamp:", DateTime.Now.ToLongDateString()));
        log.Info(String.Concat("Message:", "jwtToken.Issuer: " + jwtToken.Issuer, ";ActionMethod:", "CreateJsonWebToken", ";TimeStamp:", DateTime.Now.ToLongDateString()));

        var finalToekn = tokenHandler.WriteToken(jwtToken);
        log.Info(String.Concat("Message:", "finalToekn is " + finalToekn, ";ActionMethod:", "CreateJsonWebToken", ";TimeStamp:", DateTime.Now.ToLongDateString()));

        return finalToekn;
    }
    catch (Exception)
    {
        return null;
    }
}

There is no exception, it only returns an empty result with 200 status code.

Ideally it should generate a JWT token.

0

There are 0 answers