Home > Sivlerlight 3, RIA Services > Using asp.net Membership and profile providers with RIA Services

Using asp.net Membership and profile providers with RIA Services

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

So I wrote couple of custom providers.

MembershipProvider

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

ProfileProvider

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″ >
      <providers>
        <clear/>
        <add name=”SampleMembershipProvider1″ type=”CustomProviders.SampleMembershipProvider” connectionStringName=”constring” enablePasswordRetrieval=”true” enablePasswordReset=”true” requiresQuestionAndAnswer=”true” writeExceptionsToEventLog=”false”/>
      </providers>
    </membership>

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

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

[EnableClientAccess]
    public class Authentication : AuthenticationBase<UserBase> { }

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

 [EnableClientAccess]
    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

    [EnableClientAccess]
    public class MembershipService : DomainService
    {
        [ServiceOperation]
        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
 <Application.Services>
        <appsvc:WebUserService x:Name=”UserService” />
    </Application.Services>
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()
        {
            InitializeComponent();
            CreateUser();                             
        }

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

        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);
                service.SaveUser();

            }           
        }

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

you can download the code here

About these ads

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: