从类库项目获取UserId [英] Get UserId from class library project
问题描述
是否可以从类库项目中获取UserId?这个类库项目记录用户所做的一切并将其写入数据库.
Is there a way to get the UserId from a class library project? This class library project logs everything a user does and write it to a database.
但是我也需要UserId,除了Web项目之外,我都无法从其他任何项目中获得该ID.我还需要来自类库项目的用户ip地址.
But I need the UserId too which I can't get from any project except a web project. I also would need the user's ip address from the class library project.
任何人以前都在asp.net-mvc
项目中做到了这一点,并且可以分享如何做吗?
Anyone has done this before in an asp.net-mvc
project and could share it how to do it?
推荐答案
在我当前的项目中,我遇到了类似的情况,我这样做的方法是将UserID作为参数从MVC项目传递到我使用的任何方法中类库.
I had a similar situation in my current project, the way I did it was to pass the UserID as a parameter from the MVC project into any method I used in the class library.
编辑
public int ClassLibraryMethod(your parameters, **string userId**){}
现在,无论您在MVC项目中使用的是哪个类,都可以调用User.Identity.Name,将其分配给变量,然后将其传递给类库方法以供使用.
Now from whatever class you are working from in your MVC project, you can call User.Identity.Name, assign it to a variable, and pass it along through your class library methods for use.
string userId = User.Identity.Name;
var lib = new ClassLibrary();
lib.ClassLibraryMethod(your data here, userId){}
这将允许您通过方法从MVC项目中获取userId.
This will allow you to get the userId from the MVC project through your methods.
这篇关于从类库项目获取UserId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!