初学者的ASP登录和注销帮助

时间:2014-04-11 17:47:05

标签: c# asp.net web-services

问题1:登录页面 - 我想要一个弹出窗口,如果用户输错了信息,就会重新输入凭据。

问题2:注销功能 - 我需要在点击注销按钮时结束用户会话。有人可以告诉我完全什么放在Logout.aspx.cs和Logout.aspx页面?

登录页面的代码隐藏:

   protected void btnLogin_Click1(object sender, EventArgs e)
    {

        ThisWS.Client client = new ThisWS.Client();
        //client.Endpoint.Address = new System.ServiceModel.EndpointAddress("https://svcThisService.svc/soap");
        WSAccess.ThisWS.clsTypesAuthResult response = client.Auth(this.txtUsername.Text, this.txtPassword.Text, txtAuthCode.Text);
        client.Close();
        this.lblErrorMessage.Text = response.Error;
        this.lblToken.Text = response.Token.ToString();
        int?[] cases = response.CaseNum;

        //Session.Add("Username", this.txtUsername.Text);   //User must re-login after an hour, since the token expires.
        //Session.Add("Password", this.txtPassword.Text);
        //Session.Add("AuthCode", this.txtAuthCode.Text);

        Session.Add("Token", response.Token);
        Session.Add("TokenExpires", DateTime.Now.AddHours(1));
        Session.Add("Cases", cases);
        Session.Add("PartyNameId", response.PartyNameID);

        Response.Redirect("ListCases.aspx");

。我用于登录表单的ASPX代码:

<p class="redtext">Please use the form below to login.</p>

 <div class="">

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

    <table class="auto-style1">
        <tr>
            <td class="auto-style2">Your Username:</td></tr>
        <tr>
            <td>
                <asp:TextBox ID="txtUsername" runat="server" Width="241px" MaxLength="255"></asp:TextBox>
            </td>
            <td>&nbsp;</td>
        </tr>
        <td>&nbsp;</td>
        <tr>
            <td class="auto-style2">Your Password:</td></tr>
        <tr>
            <td>
                <asp:TextBox ID="txtPassword" runat="server" Width="239px"></asp:TextBox>
            </td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td class="auto-style2" hidden="hidden">Authentication code: </td>
            <td>
                <asp:Label ID="txtAuthCode" runat="server" Width="244px" Visible="False">000</asp:Label>
            </td>
            <td>&nbsp;</td>
        </tr>
        <tr>

            <td>
                <asp:Button ID="btnLogin" runat="server" Text="Click Here to Login" class="btn btn-primary btn-block" Width="175px" OnClick="btnLogin_Click1" />
            </td>
            <td>&nbsp;</td>
        </tr>

       </table>

</div>
</form> 
</div>

1 个答案:

答案 0 :(得分:0)

1)使用在登录按钮的OnClick事件中具有警报的功能。我会使用javascript函数来验证凭据,然后警告凭据是否不正确

e.g。

<asp:Button ID="btnLogin" runat="server" Text="Click Here to Login" class="btn btn-primary btn-block" Width="175px" OnClick="verifyCredentials()" />

在JavaScript中:

function verifyCredentials() {
    if ( credentials != correctCredentials ) {
          alert('Please Enter Correct Credentials')
    else
          //proceed

最佳实践不是使用OnClick事件。在JavaScript中使用事件侦听器或使用jQuery绑定事件:

JavaScript的:

element.addEventListener(type, handler, false);

的jQuery

$('#btnLogin').click(function() { ..... });

如果您没有JavaScript经验,请转到此链接:http://www.w3schools.com/js/DEFAULT.asp

这可以帮助您更熟悉语言和用途。

2)我不太熟悉,但StackOverflow上已经广泛讨论了这些解决方案。以下是一些指向正确方向的链接:

How to kill the Session of User when he LoggedOut in ASP.NET

How to end the user session and make sure that the user is logged out?