Here you can use Asp.net Login Control for Login Purpose with its own login and logout state.
here a full tutorial with asp login control.
Preview:
First of all you have to add Login control from the toolbox in Login.aspx page.
Code:
 
-> There is another page called Home.aspx for after successful login.If you want to logout with this login control than just Add the Logout Control from Toolbox.
LoginStatus at Home.aspx :
Code-Behind of Home.aspx.cs :
Above code is for Logout Button from Login control with it's Session State Management.
I hope You will like it this tutorial.
here a full tutorial with asp login control.
Preview:
First of all you have to add Login control from the toolbox in Login.aspx page.
Code:
            <asp:Login ID="Login1" runat="server" OnAuthenticate="ValidateUser" DisplayRememberMe="false" BackColor="#E3EAEB" BorderColor="#E6E2D8" BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="Medium" ForeColor="#333333" TextLayout="TextOnTop" TitleText="Admin Panel Log In" Width="300px" CssClass="table-condensed">
                <CheckBoxStyle CssClass="radio" Width="300px" />
                <InstructionTextStyle Font-Italic="True" ForeColor="Black" />
                <LabelStyle ForeColor="#2F6C85" />
                <LoginButtonStyle BackColor="#428bca" BorderColor="#357ebd" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="1.0em" Font-Bold="true" ForeColor="White" CssClass="btn btn-block" />
                <TextBoxStyle Font-Size="0.8em" CssClass="form-control" Height="30px" Width="250px" />
                <TitleTextStyle BackColor="#1C5E55" Font-Bold="True" Font-Size="0.9em" ForeColor="White" Font-Strikeout="False" />
                <ValidatorTextStyle Font-Strikeout="False" />
            </asp:Login>
CSS:
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <script type="text/x-jquery-tmpl" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
Now after you successfully add Login control and add css file from above, you have to code your self like below, this is an example you can do it by your self.
Code-behind of Login.aspx.cs Page:
using System.Web.Security;
protected void ValidateUser(object sender, EventArgs e)
    {
        if (Login1.UserName.ToString() == "admin" && Login1.Password.ToString() == "admin")
        {
            Session["Admin"] = Login1.UserName.ToString();
            FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);
        }
        else
        {
            Login1.FailureText = "Username OR Password is wrong.!";
        }
    }
-> After you successfully login with Username and Password it will redirect to another page, but for this you have to config the web.config file in </system.web>.
Web.Config:
<authentication mode="Forms">
      <forms defaultUrl="~/Home.aspx" loginUrl="~/Login.aspx" path="/" slidingExpiration="true" timeout="2880">
      </forms>
    </authentication>
  </system.web>
-> There is another page called Home.aspx for after successful login.If you want to logout with this login control than just Add the Logout Control from Toolbox.
LoginStatus at Home.aspx :
<asp:LoginStatus ID="LoginStatus1" runat="server" LogoutPageUrl="~/Login.aspx" />Code-Behind of Home.aspx.cs :
using System.Web.Security;
protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        if (!this.IsPostBack)
        {
            if (!this.Page.User.Identity.IsAuthenticated)
            {
                FormsAuthentication.RedirectToLoginPage();
                Session.Clear();
                Session.Abandon();
                Session["Admin"] = null;
                Response.Redirect("Login.aspx");
            }
            else
            {
                //Your Code goes here
            }
        }
    }Above code is for Logout Button from Login control with it's Session State Management.
I hope You will like it this tutorial.

0 Comments
Post a Comment