0

我正在使用 VS 2012 Professional 和 SQL Server 2012 Express。

我有两个问题。

首先:我正在尝试运行Registration.aspx,但我在MasterPage的这一行上收到错误:

 txtLoginUser.Visible = True

Object reference not set to an instance of an object.

母版页:

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<script runat="server">



    Protected Sub Page_Load(sender As Object, e As EventArgs)

        If Session("userid") = Nothing Then
            txtLoginUser.Visible = True
            txtLoginPass.Visible = True

        Else


            Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
            Dim useridComm As String = "SELECT name, surname FROM users WHERE user_id=@userid"
            Dim sqlUserID As New SqlCommand

            conn.Open()

            Dim userid As String = Session("userid")

            sqlUserID = New SqlCommand(useridComm, conn)
            sqlUserID.Parameters.AddWithValue("@userid", userid)
            Dim datareader As SqlDataReader = sqlUserID.ExecuteReader()
            If datareader.HasRows Then
                Response.Redirect("Default.aspx")
                lblLoggedIn.Text = datareader("name").ToString() & " " & datareader("surname").ToString()
            End If
            datareader.Close()
            conn.Close()

        End If
    End Sub

   Protected Sub Button1_Click(sender As Object, e As EventArgs)

        Dim loginSQL As New SqlCommand
        Dim loginComm As String

        Dim CommonFunctions As New CommonFunctions()
        Dim dec_pass As String = CommonFunctions.EncryptPassword(txtLoginPass.Text.Trim)

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")


        loginComm = "SELECT user_id FROM users WHERE username=@username and password=@password"

        conn.Open()


        loginSQL = New SqlCommand(loginComm, conn)
        loginSQL.Parameters.AddWithValue("@username", txtLoginUser.Text.ToString)
        loginSQL.Parameters.AddWithValue("@password", dec_pass)
        Dim dr As SqlDataReader = loginSQL.ExecuteReader()
        dr.Read()


        If dr.HasRows Then
            Session("userid") = dr("user_id")
        ElseIf dr.HasRows = False Then

            lblRegister.ForeColor = Drawing.Color.Red
            lblRegister.Text = "Incorrect Username/Password."
        End If
        dr.Close()
        conn.Close()

    End Sub





</script>



<head>
    <meta charset="utf-8" />
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <title></title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <link rel="stylesheet" href="style.css" type="text/css" media="screen, projection" />
</head>

<body>

    <form id="form1" runat="server">

<div id="wrapper">

    <header id="header">
        <strong>Header:</strong> Mobile CMS

        </header>


<section id="login">

    <div id="login-form">



            <asp:ContentPlaceHolder id="ContentPlaceHolder2" runat="server">

                <p>

                    <asp:Label ID="lblUsername" runat="server" Font-Bold="True" Text="U:"></asp:Label>
&nbsp;<asp:TextBox ID="txtLoginUser" runat="server" BorderStyle="None" BorderWidth="0px" Wrap="False"></asp:TextBox>
&nbsp;
                    <asp:Label ID="lblUsername0" runat="server" Font-Bold="True" Text="P:"></asp:Label>
                    <asp:TextBox ID="txtLoginPass" runat="server" BorderStyle="None" BorderWidth="0px" TextMode="Password" Wrap="False"></asp:TextBox>
&nbsp;
                    <asp:Button ID="btnLogin" runat="server" BorderStyle="None" OnClick="Button1_Click" Text="Login" />

                </p>

                <p>

                    <asp:Label ID="lblRegister" runat="server" Font-Bold="True" Font-Underline="True" ForeColor="#0000CC" Text="Register"></asp:Label>

                    <asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server">
                        <asp:Label ID="lblLoggedIn" runat="server" Text=""></asp:Label>
                    </asp:ContentPlaceHolder>

                </p>


                </asp:ContentPlaceHolder>



    </div>

</section>

<div class="navigation-bar">
       <ul class="navigation-menu">

            <li><a href="#" class="home">Home</a></li>
            <li><a href="#" class="mainsettings">Settings</a></li>
            <li><a href="#" class="profile">Profile</a>

                <ul>
                    <li><a href="#" class="messages">Messages</a></li>
                    <li><a href="#" class="settings">Profile Settings</a></li>
                </ul>

            </li>
            <li><a href="#" class="uploads">Uploads</a></li>
            <li><a href="#" class="documents">Media</a>


                <ul>
                    <li><a href="#" class="docs">Documents</a></li>
                    <li><a href="#" class="others">Others</a></li>
                </ul>

            </li>

            <li><a href="#" class="projects">Projects</a>


                <ul>
                    <li><a href="#" class="yprojects">Your Projects</a></li>
                    <li><a href="#" class="otherprojects">Other Projects</a></li>
                </ul>

            </li>

        </ul>

    </div>


    <section id="middle">

        <div id="container">
            <div id="content">
                <div>
                    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

                    </asp:ContentPlaceHolder>
                </div>
        </div>
        </div>



    </section>

    <footer id="footer">
        <strong>Footer:</strong> adsfdsgfds
    </footer>

</div>

    </form>

</body>
</html>

注册.aspx:

<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Registration.aspx.vb" Inherits="Registration" %>

<%-- Add content controls here --%>
<asp:Content ID="Content1" runat="server" contentplaceholderid="ContentPlaceHolder1">

        <p>
            <strong>Name</strong></p>
        <p>
            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        </p>
        <p>
            <strong>Surname</strong></p>
        <p>
            <asp:TextBox ID="txtSurname" runat="server"></asp:TextBox>
        </p>
        <p>
            <strong>Username&nbsp;&nbsp;
            <asp:Label ID="lblUsername" runat="server" BorderStyle="Solid" Font-Bold="True" ForeColor="Red" Visible="False"></asp:Label>
            </strong></p>
        <p>
            <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
        </p>
        <p>
            <strong>Password</strong></p>
        <p>
            <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
        </p>
        <p>
            <strong>Confirm Password</strong></p>
        <p>
            <asp:TextBox ID="txtCPassword" runat="server" TextMode="Password"></asp:TextBox>
        </p>
        <p>
            <strong>Email</strong>
            <asp:RequiredFieldValidator ID="req4" runat="server" ControlToValidate="txtEmail" Text="*" />
            <asp:RegularExpressionValidator
              ID="emailValidator"
              runat="server"
              ErrorMessage="Incorrect Email!"
              controltovalidate="txtEmail"
              validationexpression="\w+\w*\@\w+\w+\w*\.(com|edu|org|gov)" Font-Bold="True" ForeColor="Red"

              ></asp:RegularExpressionValidator>

            </p>
        <p>

            <asp:TextBox ID="txtEmail" runat="server" Width="198px"></asp:TextBox>
        </p>
        <p>
            <strong>Confirm Email</strong>

            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtCEmail" Text="*" />
             <asp:RegularExpressionValidator
              ID="RegularExpressionValidator2"
              runat="server"
              ErrorMessage="Incorrect Confirmation Email!"
              controltovalidate="txtCEmail"
              validationexpression="\w+\w*\@\w+\w+\w*\.(com|edu|org|gov)" Font-Bold="True" ForeColor="Red"

              ></asp:RegularExpressionValidator>

        </p>
        <p>
            <asp:TextBox ID="txtCEmail" runat="server" Width="191px"></asp:TextBox>



        </p>
        <p>
            <strong>Address </strong>
        </p>
        <p>
            <strong>
            <asp:TextBox ID="txtAddress1" runat="server" Width="237px"></asp:TextBox>
            </strong>
        </p>
        <p>
            <strong>
            <asp:TextBox ID="txtAddress2" runat="server" Width="238px"></asp:TextBox>
            </strong>
        </p>
        <p>
            <strong>City</strong></p>
        <p>
            <asp:TextBox ID="txtCity" runat="server" Width="143px"></asp:TextBox>
        </p>
        <p>
            <strong>Country</strong></p>
        <p>
            <asp:DropDownList ID="drpCountry" runat="server" DataSourceID="XmlDataSource1" DataTextField="iso" DataValueField="iso">
            </asp:DropDownList>
            <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/countries/countries.xml"></asp:XmlDataSource>
        </p>
        <p>
            <strong>Date of Birth&nbsp;&nbsp;
            <asp:Label ID="lblCal" runat="server" BorderStyle="Solid" Font-Bold="True" ForeColor="Red" Visible="False"></asp:Label>
            </strong></p>
        <p>
            <asp:Calendar ID="calDOB" runat="server" BackColor="White" BorderColor="#999999" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px" Width="200px" CellPadding="4" DayNameFormat="Shortest" EnableTheming="True">
                <DayHeaderStyle Font-Bold="True" Font-Size="7pt" BackColor="#CCCCCC" />
                <NextPrevStyle VerticalAlign="Bottom" />
                <OtherMonthDayStyle ForeColor="#808080" />
                <SelectedDayStyle BackColor="#666666" ForeColor="White" Font-Bold="True" />
                <SelectorStyle BackColor="#CCCCCC" />
                <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
                <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
                <WeekendDayStyle BackColor="#FFFFCC" />
            </asp:Calendar>

        </p>

        <p>
            <asp:DropDownList ID="drpMonth" runat="server" AutoPostBack="True">
            </asp:DropDownList>
            <asp:DropDownList ID="drpYear" runat="server" AutoPostBack="True">
            </asp:DropDownList>
        </p>
        <p>
            <strong>Occupation</strong></p>
        <p>
            <asp:TextBox ID="txtOccupation" runat="server" Width="143px"></asp:TextBox>
        </p>
        <p>
            <strong>Work Location</strong></p>
        <p>
            <asp:TextBox ID="txtWorkLocation" runat="server" Width="143px"></asp:TextBox>
        </p>
        <p>
            &nbsp;</p>
        <p>
            <asp:Button ID="btnRegister" runat="server" BorderStyle="Solid" Text="Register" />
        </p>
        <p>
            <strong>
            <asp:Label ID="lblFields" runat="server" BorderStyle="Solid" Font-Bold="True" ForeColor="Red" Visible="False"></asp:Label>
            </strong>
        </p>
        <p>
            &nbsp;</p>

</asp:Content>

<asp:Content ID="Content2" runat="server" contentplaceholderid="ContentPlaceHolder2">
</asp:Content>

第二:第二个问题是,当我运行 MasterPage 并输入登录详细信息时,登录将不起作用,如果我单击两次,我会得到:

输入两次登录凭据(结果)

怎么了?我怎么解决这个问题?

我想要做的是,如果具有“用户 ID”的会话可用,它将显示“欢迎,姓氏”,如果没有,则重新显示登录屏幕(2 个文本框,按钮 + 标签)

4

1 回答 1

1
  1. 您需要将登录内容移到 id 的 ContentPlaceHolder 之外ContentPlaceHolder2

  2. 由于无限重定向,您收到第二个错误。您在default.aspx并且您的母版页看到您已登录,运行您的查询,并将您重定向回default.aspx......并且循环继续。您需要摆脱该重定向。

处理身份验证的更好方法是不使用该会话变量,而只需设置一个身份验证 cookie。您可以使用FormsAuthentication.RedirectFromLoginPage 之类的东西。

所以像:

If dr.HasRows Then
    FormsAuthentication.RedirectFromLoginPage(dr("user_id"), true)

使用此 cookie,您将能够使用许多内置的 ASP.NET 功能来处理登录/身份验证。

于 2013-04-03T17:36:14.790 回答