简体   繁体   English

如何从非控制器类中的Microsoft.AspNetCore.Identity获取UserID

[英]How to get UserID from Microsoft.AspNetCore.Identity in a non controller class

Hoping someone can help me out here. 希望有人可以在这里帮助我。 I'm new to ASP, MVC and C# but trying to build a web application to learn it. 我是ASP,MVC和C#的新手,但是试图构建一个Web应用程序来学习它。 I've created the default application that creates all the controllers, views etc for users and all works well. 我创建了默认应用程序,该应用程序为用户创建了所有控制器,视图等,并且一切正常。

I've now created a class and inside that class, I need to get the userID or user name of the logged in user. 现在,我已经创建了一个类,并且在该类中,我需要获取登录用户的用户ID或用户名。 I've tried injecting UserManager and SignInManager into the constructor of the class but then any controllers calling that call complain that I'm not passing in the required parameters. 我尝试将UserManager和SignInManager注入到类的构造函数中,但是随后任何调用该调用的控制器都抱怨我没有传递所需的参数。

This may be cheeky but if someone has a basic example of how to retrieve something like the userID from inside a class, I could probably work backwards and understand what's going on. 这可能很厚脸皮,但如果有人有一个基本的示例,说明如何从类内部检索诸如userID之类的内容,那么我可能可以向后工作并了解正在发生的事情。 Been searching for hours on line but can't find any examples that work. 一直在搜索在线时间,但找不到任何有效的示例。

Using VS 2015 Asp Core Net RC2 使用VS 2015 Asp Core Net RC2

Thanks 谢谢

The user id exists in the HttpContext . 用户ID存在于HttpContext Identity just adds helpers, but you still need the HttpContext instance, which you won't have outside of a controller. 身份只是添加了助手,但是您仍然需要HttpContext实例,该实例不会在控制器外部。 This would seem to be a prime example of an XY problem, where you're asking how to do something to solve a problem in a certain way you've decided to approach it rather than asking about how to solve the problem itself. 这似乎是XY问题的一个很好的例子,您在问如何决定以某种方式解决问题,而不是询问如何解决问题本身。 Generally speaking, if you need the user id outside of the controller, you should simply pass the literal user id from the controller to whatever you're calling into, rather than trying to retrieve it from there. 一般而言,如果您需要控制器外部的用户ID,则只需将文字用户ID从控制器传递到您要调用的对象,而不是尝试从那里检索它。

This worked for me 这对我有用

Check Inside Services section of this article, to see how to get instance of HttpContextAccessor (eg _httpContextAccessor ) 检查系统内服务这个文章,来看看如何获得HttpContextAccessor的实例(例如_httpContextAccessor)

Then you may get the user id just like this: 然后,您可能会像这样获得用户标识:

long userId = ClaimsPrincipalExtensions.GetUserId(_httpContextAccessor.HttpContext.User);

Assuming that you have already written the method ClaimsPrincipalExtensions.GetUserId , if you didn't, just check this . 假设您已经编写了ClaimsPrincipalExtensions.GetUserId方法,如果没有,只需检查this即可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Microsoft.AspNetCore.Identity Identity 中使用非 ASCII 字符 - Using non-ASCII characters in Microsoft.AspNetCore.Identity Identity Microsoft.AspNetCore.Identity在Razor类库上不起作用 - Microsoft.AspNetCore.Identity not work on Razor Class Library Blazor 和 Microsoft.AspNetCore.Identity 需要电子邮件 - Blazor and Microsoft.AspNetCore.Identity Requiring Email Microsoft.AspNetCore.Identity 与 Azure ActiveDirectory - Microsoft.AspNetCore.Identity with Azure ActiveDirectory 在 .Net 标准项目中使用 Microsoft.AspNetCore.Identity? - Use Microsoft.AspNetCore.Identity in .Net Standard Project? 使用 Azure 表存储的 Microsoft.AspNetCore.Identity - Microsoft.AspNetCore.Identity using Azue Table Storage Microsoft.AspNetCore.Identity授权或身份验证与剃须刀页面 - Microsoft.AspNetCore.Identity authorization or authentification with razor pages 将 Microsoft.AspNetCore.Identity 默认表的 id 设置为 int - setting Microsoft.AspNetCore.Identity default tables's ids to int 将现有的Microsoft.AspNet.Identity DB(EF 6)迁移到Microsoft.AspNetCore.Identity(EF Core) - Migrate existing Microsoft.AspNet.Identity DB (EF 6) to Microsoft.AspNetCore.Identity (EF Core) Microsoft.Aspnet.identity.Core与Microsoft.AspNetCore.Identity之间有什么区别 - What is the difference between Microsoft.Aspnet.identity.Core vs Microsoft.AspNetCore.Identity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM