Windows Authentication with WCF RIA Services

Modified on Tuesday, 13 April 2010 08:27 AM by Daren May — Categorized as: Uncategorized

I followed the instructions outlined at the following blog post: http://www.dansoltesz.com/post/2010/01/26/WCF-Ria-Services-Active-Directory-Roles.aspx and I received a SQL exception that suggests that the service is still trying to hit a SQL repository to retrieve the user information. To work around this, I added the following to the AuthenticationService implementation:

        /// <summary>
        /// Gets details of the authenticated user.
        /// </summary>
        /// <param name="principal">
        /// The principal.
        /// </param>
        /// <returns>
        /// A populated instance of the User class.
        /// </returns>
        protected override User GetAuthenticatedUser(IPrincipal principal)
        {
            return User.CreateFromPrincipal(principal);
        }

And added this to the User class:

        /// <summary>
        /// Creates a User instance from the supplied IPrincipal.
        /// </summary>
        /// <param name="principal">
        /// The principal.
        /// </param>
        /// <returns>
        /// A populated instance of the User class.
        /// </returns>
        public static User CreateFromPrincipal(IPrincipal principal)
        {
            
            return new User
                       {
                           FriendlyName = principal.Identity.Name, 
                           Name = principal.Identity.Name,
                           Roles = System.Web.Security.Roles.GetRolesForUser(principal.Identity.Name)
                       };
        }