Get UserId from class library project

[亡魂溺海] 提交于 2019-12-25 01:53:08

问题


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.

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.

Anyone has done this before in an asp.net-mvc project and could share it how to do it?


回答1:


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.

EDIT

public int ClassLibraryMethod(your parameters, **string userId**){}

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){}

This will allow you to get the userId from the MVC project through your methods.



来源:https://stackoverflow.com/questions/23444366/get-userid-from-class-library-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!