C# Open ID Connect Generate Token from a service class

Viewed 43

I have the following code in my Startup.cs

var openIdOptions = new OpenIdConnectAuthenticationOptions
            {
                ClientId = openIdConfiguration.ClientId,
                Authority = openIdConfiguration.Authority,
                RedirectUri = openIdConfiguration.RedirectUri,
                ResponseType = openIdConfiguration.CodeFlowEnabled ? "code" : OpenIdConnectResponseTypes.IdToken,
                TokenValidationParameters = tokenValidationParameters,
                Scope = string.IsNullOrEmpty(openIdConfiguration.Scope) ? OpenIdConnectScopes.OpenIdProfile : openIdConfiguration.Scope,

                PostLogoutRedirectUri = openIdConfiguration.PostLogoutRedirectUri,

                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    AuthorizationCodeReceived = this.AuthorizationCodeReceived,
                    MessageReceived = this.MessageReceived,
                    SecurityTokenValidated = this.SecurityTokenValidated,
                    SecurityTokenReceived = this.SecurityTokenReceived,
                    AuthenticationFailed = this.AuthenticationFailed,
                    RedirectToIdentityProvider = this.RedirectToIdentityProvider
                },
                SignInAsAuthenticationType = "Cookies",
                CookieManager = new SameSiteCookieManager(new SystemWebCookieManager())
            };

....

            app.UseOpenIdConnectAuthentication(openIdOptions);


I would like to generate a token from a separate TokenService.cs, with a different configuration which will be consumed by another application.

public class TokenService
{
    public async Task<string> GenerateToken()
    {

        var openIdOptions = new OpenIdConnectAuthenticationOptions 
        {
           //similar OIDC config
        }
        
        **// How to generate a token ?** 
        
        return token;
    }
}

How to go about generating a new token when needed from my TokenService class ?

0 Answers
Related