Friday, April 27, 2012

How to use Asp.net Membership use in silverlight Business application

Reference: http://msdn.microsoft.com/en-us/gg315205

Open the project in visual studio 2010
Open the  <project>.web projects web.config file.
Find the <system.web> element
Delete the <rolemanager Enabled=”true”> element
Delete the <profile> element and all its children.
Inside the <system.web> element, add the following.
<membership defaultProvider="SlEventManagerMembershipProvider" userIsOnlineTimeWindow="15">
  <providers>
    <clear />
    <add name="SlEventManagerMembershipProvider"
         type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
         connectionStringName="SlEventManagerDb" applicationName="/SlEventManager" enablePasswordRetrieval="false"
         enablePasswordReset="true" requiresQuestionAndAnswer="true" requiresUniqueEmail="true"
         passwordFormat="Hashed" />
  </providers>
</membership>

<roleManager enabled="true" defaultProvider="SlEventManagerRoleProvider">
  <providers>
    <clear />
    <add name="SlEventManagerRoleProvider"
         type="System.Web.Security.SqlRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
         connectionStringName="SlEventManagerDb" applicationName="/SlEventManager" />
  </providers>
</roleManager>

<profile enabled="true" defaultProvider="SlEventManagerProfileProvider">
  <providers>
    <clear />
    <add name="SlEventManagerProfileProvider" connectionStringName="SlEventManagerDb"
         applicationName="/SlEventManager"
         type="System.Web.Profile.SqlProfileProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </providers>
  <properties>
    <add name="FriendlyName" />
  </properties>
</profile>
Find <connectionStrings> section of the web.config.add another entry for the authentication database.

<add name="SlEventManagerDb"
     connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SlEventManager.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"
     providerName="System.Data.SqlClient" />
Run the application.

Displaying links for user based on login
Write the following in MainPage’s constructor

  WebContext.Current.Authentication.LoggedIn += (se, ev) =>
            {
                Link3.Visibility = System.Windows.Visibility.Visible;
                Divider2.Visibility = Visibility.Visible;
            };
            WebContext.Current.Authentication.LoggedOut += (se, ev) =>
            {
                Link3.Visibility = System.Windows.Visibility.Collapsed;
                Divider2.Visibility = Visibility.Collapsed;
            };
If user logged out, user should be redirected to Home or Particular page

            WebContext.Current.Authentication.LoggedOut += (se, ev) =>
            {

                NavigationService.Navigate(new Uri("/Login", UriKind.Relative));
            };


No comments:

Post a Comment