title: Custom user authentication with Master Page in c#
body: <p>I have a created a project in <strong>Visual Studio 2017</strong> using the <strong>MasterPage</strong>.</p>
  
  <p>I have added in <strong>MasterPage</strong> the method <strong>sLogins()</strong> for user authentication.</p>
  
  <p>Now I need use this method in new <strong>Web Form</strong> with <strong>MasterPage</strong>.</p>
  
  <p>I have tried this without success because the method <strong>sLogins()</strong> is not available from <strong>MasterPage</strong> :</p>
  
  <pre><code>Mp.sLogins
  </code></pre>
  
  <p>Is not sufficient the <em>public</em> declaration of the method <strong>sLogins()</strong> to recall it in new <strong>Web Form</strong> with <strong>MasterPage</strong> ?</p>
  
  <p>Do anyone have an idea why?</p>
  
  <p>My code below.</p>
  
  <pre><code>public void sLogins()
  {
    string username = Page.User.Identity.Name.ToLower();
    string[] strusername = username.Split('\\');
  
    if (strusername.Length &gt; 0)
    {
  
      username = strusername[strusername.Length - 1];
  
      string sql = @String.Format(" SELECT * FROM ");
      sql += String.Format(" logins ");
      sql += String.Format(" WHERE ");
      sql += String.Format(" UPPER(username) IN (?); ");
  
      using (OdbcConnection myConnectionString =
          new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQLA"].ConnectionString))
      {
          using (OdbcCommand command =
              new OdbcCommand(sql, myConnectionString))
          {
              try
              {
                  if (username != null)
                  {
                      command.Parameters.AddWithValue("param1", username.ToString().ToUpper());
                      command.Connection.Open();
  
                      using (OdbcDataReader reader = command.ExecuteReader())
                      {
                          if (reader.HasRows)
                          {
                              while (reader.Read())
                              {
                                  ....
                              }
  
                          }
                          else
                          {
                              Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('....');window.location='...';", true);
                          }
                      }
                  }
                  else
                  {
                      Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('....');window.location='...';", true);
                  }
              }
              catch (Exception ex)
              {
                  throw new ApplicationException("operation failed!", ex);
              }
              finally
              {
                  command.Connection.Close();
              }
          }
      }
  
    }
  }
  </code></pre>
  
  <p>New Web Form with <strong>MasterPage</strong></p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="Mp.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %&gt;
  &lt;%@ MasterType VirtualPath="Mp.master" %&gt;
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"&gt;
  &lt;/asp:Content&gt;
  &lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"&gt;
  
  &lt;/asp:Content&gt;
  </code></pre>