locked
Custom user authentication with Master Page in c# RRS feed

  • Question

  • User-2004582644 posted

    Hi there, 

    I have a created a project in Visual Studio 2017 using the MasterPage.

    I have added in MasterPage the method sLogins() for user authentication.

    Now I need use this method in new Web Form with MasterPage.

    I have tried this without success because the method sLogins() is not available from MasterPage :

    Mp.sLogins

    Is not sufficient the public declaration of the method sLogins() to recall it in new Web Form with MasterPage ?

    Do anyone have an idea why?

    My code below.

        public void sLogins()
        {
          string username = Page.User.Identity.Name.ToLower();
          string[] strusername = username.Split('\\');
    
          if (strusername.Length > 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();
                    }
                }
            }
    
          }
        }
    

    New Web Form with MasterPage

        <%@ Page Title="" Language="C#" MasterPageFile="Mp.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
        <%@ MasterType VirtualPath="Mp.master" %>
        <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
        </asp:Content>
        <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        

    Monday, May 6, 2019 8:03 AM

Answers

  • User-893317190 posted

    Hi Chevy Marl Sunderland,

    The way you call sLogins is to call a static Method.

    If you want to call an instance method which is defined in master page , you could use Master property.

    For example , I define a method in master page.

     public partial class SiteMaster : MasterPage
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            public string masterMethod()
            {
               return Page.User.Identity.IsAuthenticated.ToString();
                
            }
        }

    Then I could call it as follows in child page.

       <%@ MasterType VirtualPath="~/Site.Master" %> 
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
     <%=Master.masterMethod() %>
    
    </asp:Content>

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 6, 2019 9:35 AM