{"id":200,"date":"2024-07-06T09:02:00","date_gmt":"2024-07-06T01:02:00","guid":{"rendered":"https:\/\/aabirtech.com\/shiblenomanblog\/?p=200"},"modified":"2024-08-10T09:09:10","modified_gmt":"2024-08-10T01:09:10","slug":"getting-logged-in-user-id-in-non-controller-class-in-asp-net-core","status":"publish","type":"post","link":"https:\/\/aabirtech.com\/shiblenomanblog\/2024\/07\/06\/getting-logged-in-user-id-in-non-controller-class-in-asp-net-core\/","title":{"rendered":"Getting logged in user ID in non-controller class in ASP.Net Core"},"content":{"rendered":"\n<p>It&#8217;s easy to retrieve the current logged-in user&#8217;s information, such as user ID, username, or email, in an ASP.Net Core controller class. However, it can be tricky to access this information in other classes, like the <code>IdentityDbContext<\/code> class. Here&#8217;s an example of how to extract the current user in a non-controller class, specifically in the <code>IdentityDbContext<\/code> class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var httpContextAccessor = this.GetService&lt;IHttpContextAccessor&gt;();\nvar userId = httpContextAccessor.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;<\/code><\/pre>\n\n\n\n<p>This example demonstrates how to retrieve the user ID. Hopefully, you find it helpful.<br><br>Happy coding \ud83d\ude42<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s easy to retrieve the current logged-in user&#8217;s information, such as user ID, username, or email, in an ASP.Net Core controller class. However, it can be tricky to access this information in other classes, like the IdentityDbContext class. Here&#8217;s an example of how to extract the current user in a non-controller class, specifically in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-200","post","type-post","status-publish","format-standard","hentry","category-asp-net-core"],"acf":[],"_links":{"self":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts\/200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/comments?post=200"}],"version-history":[{"count":2,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts\/200\/revisions"}],"predecessor-version":[{"id":204,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts\/200\/revisions\/204"}],"wp:attachment":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/media?parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/categories?post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/tags?post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}