title: C# - Forms Authentication Code-Behind w Custom Role and Membership Providers
body: <p>Unfortunately, all the examples for Forms Authentication Code Behind w/ Custom Role and Membership Providers I find online are written with a VB.NET code behind and I need a C# code behind. Please help!!!! </p>
  
  <p>I need a codebehind that will do the following: </p>
  
  <ul>
  <li>authenticate user upon login button click</li>
  <li>if user active_flag=0 (false) OR password!=@password, display error: "Access Denied" </li>
  <li>if user admin_flag=1 &amp; active flag=1 (true), redirect to admin_pages\zipsearch.aspx</li>
  <li>if user admin_flag=0 (false) &amp; active_flag=1 (true), redirect to pages\zipsearch.aspx</li>
  </ul>
  
  <p>Default.aspx Code: </p>
  
  <pre><code>    &lt;asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false"&gt;
      &lt;LayoutTemplate&gt;
          &lt;span class="failureNotification"&gt;
              &lt;asp:Literal ID="FailureText" runat="server"&gt;&lt;/asp:Literal&gt;
          &lt;/span&gt;
          &lt;asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" 
               ValidationGroup="LoginUserValidationGroup"/&gt;
          &lt;div class="accountInfo"&gt;
              &lt;fieldset class="login"&gt;
                  &lt;legend&gt;Account Information&lt;/legend&gt;
                  &lt;p&gt;
                      &lt;asp:Label ID="usernameLabel" runat="server" AssociatedControlID="username"&gt;Username:&lt;/asp:Label&gt;
                      &lt;asp:TextBox ID="username" runat="server" CssClass="textEntry"&gt;&lt;/asp:TextBox&gt;
                      &lt;asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="username" 
                           CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." 
                           ValidationGroup="LoginUserValidationGroup"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                  &lt;/p&gt;
                  &lt;p&gt;
                      &lt;asp:Label ID="passwordLabel" runat="server" AssociatedControlID="password"&gt;Password:&lt;/asp:Label&gt;
                      &lt;asp:TextBox ID="password" runat="server" CssClass="passwordEntry" TextMode="password"&gt;&lt;/asp:TextBox&gt;
                      &lt;asp:RequiredFieldValidator ID="passwordRequired" runat="server" ControlToValidate="password" 
                           CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
                           ValidationGroup="LoginUserValidationGroup"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                  &lt;/p&gt;
                  &lt;p&gt;
                      &lt;asp:CheckBox ID="RememberMe" runat="server"/&gt;
                      &lt;asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline"&gt;Keep me logged in&lt;/asp:Label&gt;
                  &lt;/p&gt;
              &lt;/fieldset&gt;
              &lt;p class="submitButton"&gt;
                  &lt;asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="LoginUserValidationGroup"/&gt;
              &lt;/p&gt;
          &lt;/div&gt;
      &lt;/LayoutTemplate&gt;
  &lt;/asp:Login&gt;
  </code></pre>
  
  <p>Web.config file: </p>
  
  <p>
      
          
          
       </p>
  
  <p>
      </p>
  
  <pre><code>&lt;authentication mode="Forms"&gt;
    &lt;forms loginUrl="~/default.aspx" timeout="2880" /&gt;
  &lt;/authentication&gt;
  
    &lt;membership&gt;
    &lt;providers&gt;
      &lt;clear/&gt;
        &lt;add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="OleConnectionStringSource"
           enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
           maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
           applicationName="/" /&gt;  
    &lt;/providers&gt;
  &lt;/membership&gt; 
  
  &lt;profile&gt;
    &lt;providers&gt;
      &lt;clear/&gt;
     &lt;add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/&gt; 
    &lt;/providers&gt;
  &lt;/profile&gt;
  
  &lt;roleManager enabled="false"&gt;
    &lt;providers&gt;
      &lt;clear/&gt;
        &lt;add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /&gt;
      &lt;add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /&gt; 
    &lt;/providers&gt;
  &lt;/roleManager&gt;
  </code></pre>
  
  <p>Default.aspx.cs code behind: </p>
  
  <pre><code>namespace ACAWebApplication
  {
    public partial class _Default : System.Web.UI.Page
    { 
       protected void Page_Load(object sender, EventArgs e)
       {
         RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
  
         // authenticate user
         // if user active_flag=0 (false) OR password!=@password, display error: "Access Denied" 
  
         // if user admin_flag=1 &amp; active flag=1 (true), redirect to admin_pages\zipsearch.aspx
         // if user admin_flag=0 (false) &amp; active_flag=1 (true), redirect to pages\zipsearch.aspx
  
        }
     }
   }
  </code></pre>
  
  <p>Thanks a lot in advance! :) </p>