Question

I have a solution which includes a website and WCF web service. Within the website I need to get the current logged on user and access a property value in their profile which specifies the username of another user (used for web service). I then need to get a property from the web service user profile (not the logged on web site user!).

So far I have this :

if (HttpContext.Current != null)
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.Profile.UserName))
            {
                serviceUsername = HttpContext.Current.Profile.GetPropertyValue("WSUserName").ToString();

                if (!string.IsNullOrEmpty(serviceUsername))
                {
                      ProfileBase profile = ProfileBase.Create(serviceUsername);
                      var siteId = profile.GetPropertyValue("SiteID");
                }
            }
        }

Which almost works, I can get the web service user profile by name but I can see the {ProfileCommon} is showing the properties of the web application instead of the web service so I am unable to see the value I need.

Any advice on how I might be able to achieve this?

Was it helpful?

Solution

OK I managed to get this working by adding additional membership and profile providers to my web.config, including an additional web service property with additional provider attribute :

     <membership>
            <providers>
            <!--(website provider here)-->
            <!--additional web service provider-->
                    <add name="WebServiceMembershipProvider"
                                type="System.Web.Security.SqlMembershipProvider"
                                connectionStringName="ApplicationServices"
                                enablePasswordRetrieval="false"
                                enablePasswordReset="true"
                                requiresQuestionAndAnswer="false"
                                requiresUniqueEmail="true"
                                maxInvalidPasswordAttempts="5"
                                minRequiredPasswordLength="6"
                                minRequiredNonalphanumericCharacters="0"
                                passwordAttemptWindow="10"
                                applicationName="MyWebService" />
            </providers>
 </membership>
 <profile>
            <providers>
                    <add name="WebServiceMembershipProvider"
                             type="System.Web.Profile.SqlProfileProvider"
                             connectionStringName="ApplicationServices"
                             applicationName="MyWebService"/>
            </providers>
             <properties>
             <!--  example website property -->
                <add name="website_property1"
                     type="string"/>
             <!--  example web service property with additional provider attribute-->
                <add name="webservice_property1"
                     type="string"
                     provider ="WebServiceMembershipProvider"/>
            </properties>
</profile>          
 <roleManager enabled="true">
            <providers>
            <!-- (website provider here) -->
            <!-- web service provider-->
             <add connectionStringName="ApplicationServices"
                     applicationName="MyWebService"
                     name="WSAspNetSqlRoleProvider"
                     type="System.Web.Security.SqlRoleProvider" />
            </providers>
</roleManager>

Then in codebehind :

Membership.ApplicationName = "MyWebService";
MembershipUser user = Membership.Providers["WebServiceMembershipProvider"].GetUser(serviceUsername, false);
profile = ProfileBase.Create(serviceUsername);
profile.Initialize(user.UserName, true);
var myProperty = profile.GetPropertyValue("webservice_property1");

hope this helps someone else!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top