0

I have an Intranet application with Windows authentication set for user authentication which works fine, only problem is that I do not want to say 'Hello, mydomain\user!' but use the user's full display name which I find in the Active Directory.

In fact I want to populate the profile with even more details from our domain, the problem is that I only want to do this AD query only once after the user has been authenticated on his first call to the application. I have all the AD and profile things working, but I do not find a good place to put the code so that it is called exactly once after login. I suspect a custom AuthorizeAttribute might be a way... Any help is greatly appreciated. Thanks!!

4

2 回答 2

1

尝试将信息存储在会话中或客户端的 cookie 或本地存储中。

于 2013-01-05T12:30:07.503 回答
0

Well, I finally came up with a solution - can this be considered as a as a valid answer? Basically I wrote a custom AuthorizationFilter and put a flag into the session to do the whole work only once. However I hoped to find an event "User_Authenticated" which is fired only once. But I guess this is more appropriate for Forms authentication.

public class ProfileUpdater : IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            // if there is a profile already in the session we do not update this
            Controller controller = filterContext.Controller as Controller;
            if (controller != null && controller.Session["ProfileUpdated"] != null)
            {
                return;
            }
            else if (controller == null) 
            {
                return;
            }

            UserPrincipal domainUser = DomainHelper.GetDomainUser(controller.User.Identity.Name);

            if (domainUser != null)
            {
                controller.Profile.SetPropertyValue("DisplayName", domainUser.DisplayName);

                controller.Session["ProfileUpdated"] = true; // just put a marker object into the session to show we alreay updated the Profile
            }

            return;
        }
    }
于 2013-01-05T16:33:21.390 回答