C#打开ID连接从服务类生成令牌

发布于 2025-02-13 07:45:46 字数 1880 浏览 1 评论 0原文

其中有以下代码

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);


我想从一个单独的tokenservice.cs生成一个令牌,

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

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

。如何在需要我的Tokenservice课程时生成新的令牌?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文