I have a Solution with three projects organized like this:

Data(entity framework) -> Service -> Web (MVC).

How can i get current loget User in my Data project from HttpContext in Controller? I need to set columns in DB like CreatedBy and ModifiedBy then creating/editng item. (authentication mode is "forms")

Thanks for advice.

有帮助吗?

解决方案 2

Ok, i found the solution. I have created a base Service class where added constructor with User parameter and then assigned to all Services the my base Service class.

其他提示

See my answer here. Get UserId from class library project

Pretty much the same thing. In your MVC controller, create a variable and store the user name like this - string user = User.Identity.Name;

Then just add a parameter to whatever methods you need the user for in your data project, and pass the user variable along to your methods.

I have created a static class which is reusable anywhere.

    public static class GetUserName
    {
        public static string Name(IPrincipal user)
        {
            return user==null? HttpContext.Current.User.Identity.Name:user.Identity.Name;
        }
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top