我要在vs 2017中创建一个网站。我有一个用于登录的Web表单,该表单已针对Web App数据库进行了验证,并且一切正常。但是,当我登录到网页时,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>
答案 0 :(得分:0)
auto_decode=False
所以我认为e.authenticated = true没有到达登录视图,因为我将自己重定向到另一页,所以您要做的只是:
if (e.Authenticated)
{
Response.Redirect("Home.aspx");
}
if (ValidateUser(Login1.UserName, Login1.Password))
{
Response.Redirect("Home.aspx");
}
else
{
e.Authenticated = false;
}
并确保将登录属性DestinationPageUrl设置为您希望用户登录后使用的属性。