首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >OpenIdDict忽略了AuthenticationProperties

OpenIdDict忽略了AuthenticationProperties
EN

Stack Overflow用户
提问于 2020-02-05 13:18:09
回答 1查看 815关注 0票数 0

我正在从.Net Framewrok迁移到Net,我使用的是OpenIdDict而不是SimpleAuthorizationProvider

我不能修改前端,所以我需要用相同的响应结构发送令牌。我正在添加AuthenticationProperties,但似乎OpenIdDict忽略了它们。

代码语言:javascript
运行
复制
public async Task<IActionResult> Token()
        {
            var request = HttpContext.GetOpenIddictServerRequest() ??
                throw new InvalidOperationException("The OpenID Connect request cannot be retrieved.");


            var user = await _userManager.FindByNameAsync(request.Username);


            var principal = await _signInManager.CreateUserPrincipalAsync(user);

            foreach (var claim in principal.Claims)
            {
                claim.SetDestinations(GetDestinations(claim, principal));
            }

            principal.SetAccessTokenLifetime(TimeSpan.FromSeconds(180));


            // Include resources and scopes, as appropriate
            var scope = new[]
            {
                Scopes.OfflineAccess,
            };
            principal.SetScopes(scope);

            var props = new AuthenticationProperties(new Dictionary<string, string>
            {
                {"id", "user.Id"},
                {"userName", "context.UserName"},
                {"firstName", "user.FirstName"},
                {"lastName", "user.LastName"},
                {"profilePicUri", "string.IsNullOrEmpty(user.ProfilePicUri) ? 'NA' : user.ProfilePicUri"},
                {"language", "user.Language ?? 'en'"},
                {"lastLoginDateTime", "user.LastLoginDateTime.ToString()"},
                {"roles", "appUserRoles"},
                {"roleNames", "appUserRoleNames"},
                {"actions", "appUserRoleActions"},
                {"azureADToken", "azureAdToken"},
                {"userSessionId", "userSessionId"},
                {"as:client_id", "context.ClientId == null ? string.Empty : context.ClientId"}
            });

            // Returning a SignInResult will ask OpenIddict to issue the appropriate access/identity tokens.
            return SignIn(principal, props, OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
        }

但我看到了这样的反应:

代码语言:javascript
运行
复制
{
    "access_token": "...",
    "refresh_token": "RqE4_j1N2yL_JYQKAupFVOigDGbs3tZCD9RifP3Suz0",
    "token_type": "Bearer",
    "expires_in": 179,
    "scope": "offline_access"
}

我也想看看其他所有的财产。I无法解码de token以从索赔中获得它们.

我的StartUp.cs

代码语言:javascript
运行
复制
services.AddOpenIddict()
                // Register the OpenIddict core components.
                .AddCore(options =>
                {
                    // Configure OpenIddict to use the Entity Framework Core stores and models.
                    options.UseEntityFrameworkCore()
                           .UseDbContext<ApplicationDbContext>();
                })
                // Register the OpenIddict server components.
                .AddServer(options =>
                {
                    // Enable the token endpoint (required to use the password flow).
                    options.SetTokenEndpointUris("/connect/token");

                    // Allow client applications to use the grant_type=password flow.
                    options.AllowPasswordFlow()
                           .AllowRefreshTokenFlow();


                    // Accept requests sent by unknown clients (i.e that don't send a client_id).
                    // When this option is not used, a client registration must be
                    // created for each client using IOpenIddictApplicationManager.
                    options.AcceptAnonymousClients();

                    // Register the signing and encryption credentials.
                    options.AddDevelopmentEncryptionCertificate()
                           .AddDevelopmentSigningCertificate();

                    // Register the ASP.NET Core host and configure the ASP.NET Core-specific options.
                    options.UseAspNetCore()
                           .EnableStatusCodePagesIntegration()
                           .EnableAuthorizationEndpointPassthrough()
                           .EnableLogoutEndpointPassthrough()
                           .EnableTokenEndpointPassthrough()
                           .EnableUserinfoEndpointPassthrough()
                           .EnableVerificationEndpointPassthrough()
                           .DisableTransportSecurityRequirement();
                })
                // Register the OpenIddict validation components.
                .AddValidation(options =>
                {
                    // Import the configuration from the local OpenIddict server instance.
                    options.UseLocalServer();

                    // Register the ASP.NET Core host.
                    options.UseAspNetCore();
                });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-08 00:54:07

必须使用事件模型显式添加所需的自定义属性。以下是你如何做到的:

代码语言:javascript
运行
复制
services.AddOpenIddict()
    .AddServer(options =>
    {
        options.AddEventHandler<ProcessSignInContext>(builder =>
        {
            builder.UseInlineHandler(context =>
            {
                // Retrieve the AuthenticationProperties set from the authorization controller
                // and add a "custom_property" containing the value of the "property" property.
                var properties = context.Transaction.GetProperty<AuthenticationProperties>(
                    typeof(AuthenticationProperties).FullName);

                context.Response["custom_property"] = properties?.GetString("property");

                return default;
            });
        });
    });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60076755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档