繁体   English   中英

用户登录后,ASP.net loginView控件不会更新

[英]ASP.net loginView control does not updating after user login

我在vs 2017中创建了一个网站。我有一个用于登录的Web表单,该表单已经过Web应用程序数据库验证,一切似乎都有效。 但是,当我登录到网页时,loginView不会更改。 我期望用户已经成功登录到网页中进行某种显示。

这是我的代码:

public partial class _Default : System.Web.UI.Page
{

 protected void Page_Load(object sender, EventArgs e)
 {

 }
private string mycon = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
 private bool ValidateUser(String username, String password)
 {
    bool status;
    SqlConnection con = new SqlConnection(mycon);
    String myquery = "select * from customer";
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = myquery;
    cmd.Connection = con;
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = cmd;
    DataSet ds = new DataSet();
    da.Fill(ds);
    String uname;
    String pass;
    uname = ds.Tables[0].Rows[0]["userName"].ToString();
    pass = ds.Tables[0].Rows[0]["password"].ToString();
    con.Close();
    if (uname == username && pass == password)
    {
        Session["username"] = uname;
        status = true;
    }
    else
    {
        status = false;
    }
    return status;
 }
 protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
 {
    if (e.Authenticated)
    {
        Response.Redirect("Home.aspx");
    }
    if (ValidateUser(Login1.UserName, Login1.Password))
    {
        Response.Redirect("Home.aspx");
    }
    else
    {
        e.Authenticated = false;
    }


 }

}

这是我的login.aspx

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script src="jquery/jQuery.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" 
Runat="Server">
<asp:Login ID="Login1" runat="server" CreateUserText="Register" 
CreateUserUrl="~/Registeration.aspx" DestinationPageUrl="~/home.aspx" 
Width="1539px" OnAuthenticate="Login1_Authenticate" InstructionText="please 
enter your Username and Password to Login">
    <LayoutTemplate>
        <table cellpadding="1" cellspacing="0" style="border-collapse: 
 collapse;">
            <tr>
                <td>
                    <table cellpadding="0" style="width:1637px;">
                        <tr>
                            <td align="center" colspan="2">Log In</td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2">please enter your Username and Password to Login</td>
                        </tr>

                   <tr>
                            <td align="right">
                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <asp:CheckBox ID="RememberMe" runat="server" 
                                Text="Remember me next time." />
                            </td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2" 
                             style="color:Red;">
                                <asp:Literal ID="FailureText" runat="server" 
                  EnableViewState="False"></asp:Literal>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <asp:Button ID="LoginButton" runat="server" 
 CommandName="Login" Text="Log In" ValidationGroup="Login1" Width="120px" />
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <asp:HyperLink ID="CreateUserLink" 
  runat="server" NavigateUrl="~/Registeration.aspx">Register</asp:HyperLink>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </LayoutTemplate>
</asp:Login>
</asp:Content>

这是我的home.aspx用户登录后会去的地方:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="home.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <table border="1" style="width:100%" height="310">
        <tr>
                <th colspan="7">
                    <h1 style="text-align:center;">Welcome to BEAUTIC</h1>
                    <p style="text-align:center;">where every girl can have a healthy and beautiful skin</p>
        </th>
                <th colspan="3">
                    <asp:image runat="server" Height="250px" ImageUrl="~/images/makeup.png" Width="300px"></asp:image>
                </th>
            </tr>
                    </table>      
</asp:Content>

这个页面没有代码。

这是我的LoginView控件(它位于母版页中):

<th colspan="1" class="auto-style1">
                    <asp:LoginView ID="LoginView1" runat="server">
                        <AnonymousTemplate>
                            Hello,<br />
                            <asp:LoginStatus ID="LoginStatus1" runat="server" CssClass="auto-style3" />
                        </AnonymousTemplate>
                        <LoggedInTemplate>
                            <span class="auto-style3">Welcome back,</span><br class="auto-style2" />
                            <asp:LoginName ID="LoginName1" runat="server" CssClass="auto-style3" />
                            <br />
                            <asp:LoginStatus ID="LoginStatus2" runat="server" CssClass="auto-style2" />
                        </LoggedInTemplate>
                    </asp:LoginView>
                </th>

我终于弄明白了 。 所以我的错误是在此语句的页面后面的login.aspx代码中:

if (e.Authenticated)
    {
        Response.Redirect("Home.aspx");
    }
    if (ValidateUser(Login1.UserName, Login1.Password))
    {
        Response.Redirect("Home.aspx");
    }
    else
    {
        e.Authenticated = false;
    }

所以我认为e.authenticated = true没有到达登录视图,因为我将自己重定向到另一个页面,所以你需要做的就是:

if 
    if (ValidateUser(Login1.UserName, Login1.Password))
    {
        e.Authenticated = true;
    }
    else
    {
        e.Authenticated = false;
    }

并确保将登录属性DestinationPageUrl设置为您希望用户在登录后访问的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM