Friday, March 30, 2018

How To Implement Forms-Based Authentication in ASP.NET

This Is A Simple Example Of How To Create Form Authentication In Asp.net.
x




1.Default.aspx.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public bool check()
    {
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Users\Bhavesh\Documents\Visual Studio 2010\WebSites\authentication\App_Data\Database.mdf';Integrated Security=True;User Instance=True");
        string select = "select * from login where username=@username and password=@password";
        con.Open();
        SqlCommand cmd = new SqlCommand(select, con);
       
        cmd.Parameters.Add("@username", TextBox1.Text);
        cmd.Parameters.Add("@password", TextBox2.Text);
        Object obj = cmd.ExecuteScalar();
        con.Close();
        if (obj != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (check())
        {

            Response.Write("<script>alert('you are authorize user')</script>");
        }
        else
        {
            Response.Write("hello");
        }
    }
}

2 Default.aspx File


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

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    username:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
    password:<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox><br />
    <asp:Button ID="Button1" runat="server" Text="Login" onclick="Button1_Click"></asp:Button>
    </div>
    </form>
</body>
</html>


3 Web.config File

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
<appSettings/>
<connectionStrings>
<add name="constring" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Users\Bhavesh\Documents\Visual Studio 2010\WebSites\authentication\App_Data\Database.mdf';Integrated Security=True;User Instance=True"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<authentication mode="Forms">
<forms name="login" loginUrl="Default.aspx" timeout="30">
</forms>
</authentication>
</system.web>
</configuration>

No comments:

Post a Comment

Generate Even Numbers in a Range In PHP.

$start = 1; $end = 20; for ($i = $start; $i <= $end; $i++) {     if ($i % 2 == 0) {         echo $i . " is even.<br>";   ...