문제

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