Asp.net core Identity server 4:访问asp.net webform.net 4.5

Asp.net core Identity server 4:访问asp.net webform.net 4.5,asp.net-core,identityserver4,Asp.net Core,Identityserver4,我使用Identity serve4进行用户身份验证和授权,我的一个客户端是asp.net webform,它是在.net 4.5上编写的。当用户试图访问受保护的Web表单时,我将用户重定向到identity server进行身份验证。但是在身份验证之后,有一个基于当前记录的userid的逻辑,为了获取当前用户登录信息,我必须调用一些需要令牌的令牌端点?我在登录后检查了webform,发现有一些auth Cookie。现在我的问题是如何从中获取令牌?在asp.net core上,我们使用下面的h

我使用Identity serve4进行用户身份验证和授权,我的一个客户端是asp.net webform,它是在.net 4.5上编写的。当用户试图访问受保护的Web表单时,我将用户重定向到identity server进行身份验证。但是在身份验证之后,有一个基于当前记录的userid的逻辑,为了获取当前用户登录信息,我必须调用一些需要令牌的令牌端点?我在登录后检查了webform,发现有一些auth Cookie。现在我的问题是如何从中获取令牌?在asp.net core上,我们使用下面的httpcontext方法获得访问令牌,但是如何使用.net 4.5在asp.net webform上获得相同的访问令牌呢

var access_token = await HttpContext.GetTokenAsync("access_token")

最简单的方法是在身份验证后将访问令牌保存在cookie中。将客户端上的代码更改为如下所示:

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                
                ResponseType = "id_token token",
                Scope = "openid profile api1",
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    SecurityTokenValidated = n =>
                    {
                        n.AuthenticationTicket.Identity.AddClaim(new Claim("access_token", n.ProtocolMessage.AccessToken));
                        return Task.FromResult(0);
                    },
                }
            });
var accessToken = user.FindFirst("access_token").Value;
然后您可以从当前用户的声明中检索access_令牌,如下所示:

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                
                ResponseType = "id_token token",
                Scope = "openid profile api1",
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    SecurityTokenValidated = n =>
                    {
                        n.AuthenticationTicket.Identity.AddClaim(new Claim("access_token", n.ProtocolMessage.AccessToken));
                        return Task.FromResult(0);
                    },
                }
            });
var accessToken = user.FindFirst("access_token").Value;
我在这里详细解释了:

编辑:

要设置Identity.Name,请添加此代码:

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                ...
                TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
                {
                    NameClaimType = "name"
                }, // This is to set Identity.Name                 
            });

非常感谢您的回复。实际上,我正试图使用UserInfoClient从asp.net webform(.net 4.5)调用/connect/userinfo端点。但这在.NET4.5上似乎不受支持。您是否有使用HttpClient而不是UserInfoClient来获取userinfo详细信息的示例代码演示?Thank/connect/userinfo只是一个get端点,需要授权头上的承载者:-要调用它,您可以使用如下内容
var client=new HttpClient();client.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“承载者”,accessToken);var response=wait client.GetAsync(“http://localhost:5001/connect/userinfo");事实上,我正试图遵循这一点,这里userinfo结果返回声明,并将该返回声明添加到声明中。我试图模仿HttpClient的相同行为。使用我发布的代码
n.ProtocolMessage.AccessToken
作为访问令牌-响应类似于
{“sub”:“248289761001”,“name”:“Bob Smith”,“given_name”:“Bob”,“family_name”:“Smith”,“role”:[“user”,“admin”]}
您可以将其反序列化为任何对象并使用it@LilRazi运气好吗?