Using Membership and profile providers with RIA Services

After reading about RIA services. I wanted to write some sample code to hook into Membership and profile providers.

So I wrote couple of custom providers.


I had a simple table with UserID,UserName,Email and password columns
I did not bother with all the different overloads. I have just added code in few places like Initialize, GetUser, ValidateUser and CreateUser


I created a table which will store 3 values for each user, Theme, City and State

again I did not code for all the different overloads. I have added code for Initialize, SetPropertyValues, GetPropertyValues

once I had these providers in place

I added a Silverlight application with Webproject

and modified the Web.config file to add my providers

 <membership defaultProvider=”SampleMembershipProvider1″ >
        <add name=”SampleMembershipProvider1″ type=”CustomProviders.SampleMembershipProvider” connectionStringName=”constring” enablePasswordRetrieval=”true” enablePasswordReset=”true” requiresQuestionAndAnswer=”true” writeExceptionsToEventLog=”false”/>

    <profile defaultProvider=”SampleProfileProvider1″>
        <add name=”SampleProfileProvider1″ type=”CustomProviders.SampleProfileProvider” connectionStringName=”constring” />
        <add name=”Theme” type=”System.Int32″ allowAnonymous=”true”/>
        <add name=”City” type=”System.String” allowAnonymous=”true”/>
        <add name=”State” type=”System.String” allowAnonymous=”true”/>

The next step is to add a new DomainService to the webproject
Adding the following code will get us started

    public class Authentication : AuthenticationBase<UserBase> { }

as we have a profile also,created another class User inheriting from UserBase

    public class Authentication : AuthenticationBase<User> { }
    public class User : UserBase
        public int Theme { get; set; }
        public string City { get; set; }
        public string State { get; set; }

I added the following class so that we can have a Page(UI) in Silverlight which allows for user Registration

    public class MembershipService : DomainService
        public void AddUser(string userName, string password, string email)
            MembershipCreateStatus createStatus;

            Membership.CreateUser(userName, password, email, null, null, true, out createStatus);
            if (createStatus != MembershipCreateStatus.Success)
                throw new DomainServiceException(createStatus.ToString());

We also need to add a Service to App.XAML
        <appsvc:WebUserService x:Name=”UserService” />
The sample does not have any UI, but when the page is loaded it will create a user and login the user as you can see from the code below

 public partial class MainPage : UserControl
        UserService service;
        public MainPage()

        void CreateUser()
            MembershipContext ctx = new MembershipContext();
            ctx.AddUserCompleted += new EventHandler<System.Windows.Ria.Data.InvokeEventArgs>(ctx_AddUserCompleted);
            ctx.AddUser(“user1”, “test”, ““);
        void ctx_AddUserCompleted(object sender, System.Windows.Ria.Data.InvokeEventArgs e)
            MessageBox.Show(“User added”);

        void Login()
            service = App.Current.Services[“UserService”] as UserService;
            service.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(service_LoginCompleted);
            service.Login(“user1”, “test”);
        void service_LoginCompleted(object sender, LoginCompletedEventArgs e)
            User user = (service.User as User);
            if (string.IsNullOrEmpty(user.City))
                user.City = “dallas”;
                user.State = “TX”;
                user.Theme = 100;
                service.SaveUserCompleted += new EventHandler<SaveUserCompletedEventArgs>(service_SaveUserCompleted);


        void service_SaveUserCompleted(object sender, SaveUserCompletedEventArgs e)
            MessageBox.Show(“login success and profile properies set”);

you can download the code here


One thought on “Using Membership and profile providers with RIA Services

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s