Speed up development with full-stack environments for every branch.

Learn More

How To Use Forms Authentication In ASP.NET [login]

5642 Runs 43852 Views 10835 Copies
Saved

Saved

mirang 126

mirang
published 4 years ago

<%@ Page Language="C#"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How To Use Forms Authentication In ASP.NET</title>
    <script language="C#" runat="server">
        public void Page_Load(Object sender, EventArgs e)
        {
            //Print the time when the page loaded initially
            Response.Write("The Page Loaded at: " + DateTime.Now.ToLongTimeString());
        }
        public void btnValidate_Click(Object sender, EventArgs args)
        {
            //perform validation for the username and password
            if (ValidateUser(txtUserId.Text, txtPassword.Text))
            {
                //Call FormsAuthentication.RedirectFromLoginPage, which would set a cookie for validation
                //and depending on the ReturnUrl value in the URL redirect user back to that page(here default.aspx)
                Session["UserId"] = txtUserId.Text;
                FormsAuthentication.RedirectFromLoginPage(txtUserId.Text, true);
            }
        }
        private bool ValidateUser(string userName, string password)
        {
            //ideally this would be validating against a datastore like a database or something else
            //for demo purpose we have done some hard code validation
            //so it will erturn true is username and password = demo
            return (userName == "demo" && password == "demo");
        }
    </script>
    <style type="text/css">
    .button
    {
      border-radius:4px 4px 4px 4px;
      height:30px;
      padding:5px;
      font-size:14px;
      background-color:#6ca21e;
      color:#FFFFFF
    }
    body
    {
      background-color:#32373a;
      color:#FFFFFF;
    }
    #mainBody
    {
      background-color:#FFFFFF;
      height:100%;
      color:#32373a;
    }
    .UCHelp
    {
        height:150px;margin-top:20px;
        font-weight:bold;
    }
    .UserControlDiv
    {
        width:300px;margin:10px;padding:10px;border:1px solid Gray;background-color:#fdd136;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div id="mainBody">
        <h1>
            How To Use Forms Authentication In ASP.NET
        </h1>
        <br />
        For login the username is <b>demo</b> and the password is <b>demo</b>
        <br />
        <div class="UserControlDiv">
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>
            User Id : 
        </td>
        <td>
            <asp:TextBox ID="txtUserId" Width="200" runat="server" />
        </td>
    </tr>
    <tr>
        <td colspan="2" style="height:5px" />
    </tr>
    <tr>
        <td>
            Password : 
        </td>
        <td>
            <asp:TextBox ID="txtPassword" Width="200" TextMode="Password" runat="server" />
        </td>
    </tr>
    <tr>
        <td colspan="2" style="height:5px" />
    </tr>
    <tr>
        <td>
            
        </td>
        <td>
            <asp:Button runat="server" OnClick="btnValidate_Click" ID="btnValidate" CssClass="button" Text="Login" />
        </td>
    </tr>
</table>
</div>
    </div>
    </form>
</body>
</html>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).