{"id":974,"date":"2022-10-26T15:24:53","date_gmt":"2022-10-26T05:24:53","guid":{"rendered":"https:\/\/www.nickdu.com\/?p=974"},"modified":"2022-10-26T15:24:53","modified_gmt":"2022-10-26T05:24:53","slug":"get-user-token-and-user-info-from-identityserver4","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=974","title":{"rendered":"Get user token and user\u00a0info from IdentityServer4"},"content":{"rendered":"\n<p><a href=\"https:\/\/identityserver4.readthedocs.io\/en\/latest\/\" target=\"_blank\" rel=\"noreferrer noopener\">IdentityServer4<\/a> is an OpenID Connect and OAuth 2.0 framework for ASP.NET Core.<\/p>\n\n\n\n<p>Use the&nbsp;<strong>PasswordTokenRequest<\/strong>&nbsp;and&nbsp;<strong>RequestPasswordTokenAsync<\/strong>&nbsp;to get the access token. replace&nbsp;<em>clientId<\/em>&nbsp;and&nbsp;<em>secret<\/em>&nbsp;with the values from your Identity Server, then use the&nbsp;<strong>UserInfoRequest<\/strong>&nbsp;and pass your access token to&nbsp;<strong>GetUserInfoAsync<\/strong>&nbsp;to get the user claims:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class TokenService\n{\n    private DiscoveryDocumentResponse _discDocument { get; set; }\n    public TokenService()\n    {\n        using (var client = new HttpClient())\n        {\n            _discDocument = client.GetDiscoveryDocumentAsync(\"http:\/\/localhost:5000\/.well-known\/openid-configuration\").Result;\n        }\n    }\n    public async Task&lt;TokenResponse> GetToken(string userName, string password)\n    {\n        using (var client = new HttpClient())\n        {\n            var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest\n            {\n                Address = _discDocument.TokenEndpoint,\n                ClientId = \"clientId\",\n                ClientSecret = \"secret\",\n                Scope = \"openid\",\n                GrantType = \"password\",\n                UserName = userName,\n                Password = password\n            });\n\n            if (tokenResponse.IsError)\n            {\n                throw new Exception(\"Token Error\");\n            }\n            return tokenResponse;\n        }\n    }\n\n    public async Task&lt;string> GetUserInfo(string accessToken)\n    {\n        using (var client = new HttpClient())\n        {\n            var response = await client.GetUserInfoAsync(new UserInfoRequest()\n            {\n                Address = _discDocument.UserInfoEndpoint,\n                Token = accessToken\n            });\n\n            if (response.IsError)\n            {\n                throw new Exception(\"Invalid username or password\");\n            }\n            return response.Raw;\n        }\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>IdentityServer4 is an OpenID Connect and OAuth 2.0 framework for ASP.NET Core. Use the&nbsp;PasswordTokenRequest&nbsp;and&nbsp;RequestPasswordTokenAsync&nbsp;to get the access token. replace&nbsp;clientId&nbsp;and&nbsp;secret&nbsp;with the values from your Identity Server, then use the&nbsp;UserInfoRequest&nbsp;and pass your access token to&nbsp;GetUserInfoAsync&nbsp;to get the user claims:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,2],"tags":[],"class_list":["post-974","post","type-post","status-publish","format-standard","hentry","category-net","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=974"}],"version-history":[{"count":0,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/974\/revisions"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}