assigning a name to the user accessing next pages : FormsAuthentication « Login Security « ASP.Net






assigning a name to the user accessing next pages


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Please, log in</title>
</head>

<body>
  <asp:label runat="server" id="errorMsg"/><br>
  <form id="Form1" runat="server">
    <table>
    <tr>
      <td><b>User ID</b></td>
      <td><asp:textbox runat="server" text="" id="userName" /></td></tr>
    <tr>
      <td><b>Password</b></td>
      <td><asp:textbox runat="server" text="" id="passWord" textmode="password" /></td></tr>
    </table>
    <asp:button ID="Button1" runat="server" text="Log In..." onclick="LogonUser" />
  </form>
</body>
</html>

File: Login.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;



public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SetFocus("userName");
    }

    protected void LogonUser(object sender, EventArgs e)
    {
        bool bAuthenticated = false;
        string user = userName.Text;
        string pswd = passWord.Text;

        // Custom authentication
        bAuthenticated = AuthenticateUser(user, pswd);
        if (bAuthenticated)
            FormsAuthentication.RedirectFromLoginPage(user, false);
        else
            errorMsg.Text = "Sorry, that's not it.";
    }

    private bool AuthenticateUser(string username, string pswd)
    {
        // No authentication--just trust everyone!
        return true;
    }
}

 








Related examples in the same category

1.FormsAuthentication.Authenticate (C#)
2.FormsAuthentication.RedirectFromLoginPage (VB)
3.Checking credentials in SQL Server (VB)
4.Checking credentials in SQL Server (C#)
5.FormsAuthentication.SignOut()
6.Hash password
7.Save user account to an XML file