Assigning a name to the user and accessing next pages : FormsAuthentication « Authentication Authorization « ASP.NET Tutorial

Home
ASP.NET Tutorial
1.ASP.Net Instroduction
2.Language Basics
3.ASP.net Controls
4.HTML Controls
5.Page Lifecycle
6.Response
7.Collections
8.Validation
9.Development
10.File Directory
11.Sessions
12.Cookie
13.Cache
14.Custom Controls
15.Profile
16.Configuration
17.LINQ
18.ADO.net Database
19.Data Binding
20.Ajax
21.Authentication Authorization
22.I18N
23.Mobile
24.WebPart
25.XML
ASP.NET Tutorial » Authentication Authorization » FormsAuthentication 
21.10.9.Assigning a name to the user and accessing next pages
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.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>
  <b style="color:red;"><asp:label runat="server" id="errorMsg"/></b>
  <br /><br />
  
  <div id="pageContent">
      <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>
  </div>
</body>
</html>

File: Default.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)
    {
       this.SetFocus("userName");
    }

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

        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)
    {
        return true;
    }
}
21.10.FormsAuthentication
21.10.1.Configuring Forms Authentication
21.10.2.Use the web configuration file to change the name of the authentication cookie.
21.10.3.Using Cookieless Forms Authentication
21.10.4.Using Sliding Expiration with Forms Authentication
21.10.5.Set user name with FormsAuthentication.SetAuthCookie
21.10.6.Validate a user with FormsAuthentication.Authenticate
21.10.7.Using Forms Authentication Across Domains: Query String Authenticate
21.10.8.Web configuration file contains a list of usernames and passwords.
21.10.9.Assigning a name to the user and accessing next pages
21.10.10.Principal Login
21.10.11.Logout
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.