Saturday 30 April 2011

Membership, Roles And Profile Settings In Web.Config

<system.web>
    ... authentication & authorization settings ...

    <membership defaultProvider="CustomizedProvider">
      <providers>
         <add name="CustomizedProvider"
              type="System.Web.Security.SqlMembershipProvider"  
              connectionStringName="MyDB"
              applicationName="/"
              minRequiredPasswordLength="5"
              minRequiredNonalphanumericCharacters="0" />
      </providers>
    </membership>

<roleManager enabled="true   cookieProtection="All">
<providers>
<add name="RainbowSqlRoleProvider"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="csRainbowAspNetDb"
applicationName="/" />
</providers>
</roleManager>

<profile defaultProvider="SqlProfileProvider">
    <providers>
      <remove name="AspNetSqlProfileProvider"/>
      <add name="SqlProfileProvider" 
       type="System.Web.Profile.SqlProfileProvider" 
       connectionStringName="LocalSqlServer"/>
    </providers>
   <properties>
      <add name="FirstName" type="string"/>
      <add name="LastName" type="string"/>
      <add name="AdvancedMode" type="bool"/>
      <group name="Address">
        <add name="Street" type="string"/>
        <add name="City" type="string"/>
        <add name="State" type="string"/>
        <add name="Zip" type="string"/>
      </group>    </properties>  </profile>
How To Save Profile Information in DB
In profile.aspx.cs file-
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MembershipUser user = Membership.GetUser();
            ProfileCommon profile = (Profile as ProfileCommon).
GetProfile(user.UserName);
            txtFirstName.Text = profile.FirstName;
            txtLastName.Text = profile.LastName;
            txtMobile.Text = profile.MobileNo;
        }
        
    }
protected void btnSubmit_Click(object sender, EventArgs e)
    {
    MembershipUserCollection allUsers = Membership.GetAllUsers();

        foreach (MembershipUser us in allUsers)
        {
ProfileCommon prof = (HttpContext.Current.Profile as ProfileCommon)
.GetProfile(us.UserName);
if (prof.MobileNo == txtMobile.Text && us.UserName != Membership.
GetUser().UserName)
            {
     lablError.Text = "<br/>Phone Number is already assign to 
another user";
                return;
            }
        }
   MembershipUser user = Membership.GetUser();
 ProfileCommon profile = (Profile as ProfileCommon).
GetProfile(user.UserName);
        profile.FirstName = txtFirstName.Text;
        profile.LastName = txtLastName.Text;
        profile.MobileNo = txtMobile.Text;
        profile.Save();
        clearAll();
        lablError.Text = "Profile Updated Successfully";
    }

No comments:

Post a Comment