Question

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.

Était-ce utile?

La solution 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.

Autres conseils

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;
        }
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top