将失败的用户名记录到ASP.NET中的txt文件

时间:2015-09-12 14:18:52

标签: asp.net

尝试捕获从登录控制用户名文本框输入的用户名,并写入单独的文本文件以供审阅。当用户输入错误的用户名时,我需要找出输入的内容。我认为这需要在后面的代码中完成,并且无法在asp.net控件中完成?

<asp:Login ID="Login1" runat="server" Width="760px">
     <LayoutTemplate>

                     <table border="0" cellpadding="0" style="width: 760px">

                         <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="ctl00$Login1">*</asp:RequiredFieldValidator>
                             </td>

                             <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="ctl00$Login1">*</asp:RequiredFieldValidator>
                             </td>
                            <td align="right">
                                 <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="ctl00$Login1" /></td>
                         </tr>
                          </table>
                         <table border="0" cellpadding="0" style="width: 760px">
                             <tr>
                             <td style="height: 16px; width: 346px; text-align: center;">
                                 <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal></td>

                             <td align="center" style="color: red; height: 16px; text-align: right;">
                                 <asp:HyperLink ID="ForgotPassword" runat="server" NavigateUrl="~/ForgotPassword.aspx">Forgot Password?</asp:HyperLink>&nbsp;</td>
                             </tr>
                         </table>
     </LayoutTemplate>
</asp:Login>

1 个答案:

答案 0 :(得分:0)

您需要在后面的代码中处理此问题。最好的地方可能是登录按钮点击处理程序:

protected void LoginButton_Click(object sender, EventArgs e)
{
    string username = Request.Form["UserName"];
    string filepath = Server.MapPath("~") + "debug.txt";

    using (StreamWriter writer = File.CreateText(filepath))
    {
        writer.WriteLine(username);
    }
}

未经测试,但总的来说这是一个相当简单的操作。显然这只会写入LAST尝试登录到文件中,它不会保留正在运行的日志。当然,你只需要将写入更改为附加。

修改

糟糕,我的阅读理解能力差。 VB.NET版本:

Protected Sub LoginButton_Click(sender As Object, e As EventArgs)
    Dim username As String = Request.Form("UserName")
    Dim filepath As String = Server.MapPath("~") + "debug.txt"

    Using writer As StreamWriter = File.CreateText(filepath)
        writer.WriteLine(username)
    End Using
End Sub