Monday, June 11, 2012

How to load Login page for Authenticate user



    Step:-1

    Design a Master page as required to our need and just add  this code  in the MasterPage.cs

  string userId;
    protected void Page_Load(object sender, EventArgs e)
    {
        //long userId = Convert.ToInt64(Session["UserId"]);
        if (Session["UserId"] != null)
        {
            userId = Session["UserId"].ToString();
        }

        if (!IsPostBack)
        {

            if (userId == "" || userId == null)
            {

                Response.Redirect("~/FrmLoginWithoutBackToHistory.aspx", false);

            }

        }
    }
    protected void Page_Init(object sender, EventArgs e)
    {

        Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));

        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        Response.Cache.SetNoStore();
       

    }

    protected void lnkLogout_Click1(object sender, EventArgs e)
    {
        Session.Clear();
        Session.Abandon();
        Response.Redirect("~/FrmLoginWithoutBackToHistory.aspx", false);
    }

 Step:-2

    Design a Login page as required to our need

     Design View 

     <form id="form1" runat="server">
    <div>
 
        <table class="style1" bgcolor="#999966">
            <tr>
                <td colspan="2" style="text-align: center">
                    <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Login Page"></asp:Label>
                </td>
           
            </tr>
            <tr>
                <td width="40%">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td style="text-align: right">
                    <asp:Label ID="Label2" runat="server" Text="User ID :"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TxtUser" runat="server" Height="22px" Width="292px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="text-align: right">
                    <asp:Label ID="Label3" runat="server" Text="Password :"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TxtPassword" runat="server" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Button ID="BtnSubmit" runat="server" BackColor="#0066FF" Font-Bold="True"
                        onclick="BtnSubmit_Click" Text="Log In" />
                </td>
            </tr>
        </table>
 
    </div>
    </form>


   C# Code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class FrmLoginWithoutBackToHistory : System.Web.UI.Page
{
    string user = "satya";
    string pass = "1234";
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
//  we can code here for Database table
        if (TxtUser.Text.Trim() == user && TxtPassword.Text.Trim() == pass)
        {
            Session["UserId"] = TxtUser.Text.Trim();
            Response.Redirect("Default.aspx", false);
        }
    }
}

 to clear  the session for more security in the web.config file

<system.web>
    <sessionState cookieless="true"></sessionState>
</system.web>




No comments:

Post a Comment