Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > บทความจากสมาชิก > (C#) ASP.NET User Authentication/Login Form แบบ SQL Server + Windows Authentication



 
Clound SSD Virtual Server

(C#) ASP.NET User Authentication/Login Form แบบ SQL Server + Windows Authentication

บทความเรื่อง (C#) ASP.NET User Authentication/Login Form เป็นตัวอย่างการ Login Form โดยใช้ฐานข้อมูล SQL Server บน ASP.NET Web Site และมีการนำ Session เข้ามาตรวจสอบการ Login ของ User ด้วยครับ

ASP.NET Login Control



Login.aspx

<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Page Language="C#" Debug="true" %>

<script runat="server">

	SqlConnection objConn;
	SqlCommand objCmd;

    public void btnLogin_Click(object sender, EventArgs e)
	{
		String strConnString;
        String strSQL;
        strConnString = "Server= localhost\\SQLEXPRESS; Database= mydatabase; Integrated Security=True;";
		objConn = new SqlConnection(strConnString);
		objConn.Open();

        int intNumRows = 0;
        strSQL = "SELECT COUNT(*) FROM member WHERE Username = '" + this.txtUsername.Text + "' AND [Password] = '" + this.txtPassword.Text + "' ";
        objCmd = new SqlCommand(strSQL, objConn);
        intNumRows = Convert.ToInt32(objCmd.ExecuteScalar());

        if (intNumRows > 0)
        {
            Session["strUser"] = this.txtUsername.Text;
            Response.Redirect("Page1.aspx");
        }
        else
        {
            Response.Redirect("Login.aspx");
            Response.End();
        }
        objConn.Close();
        objConn = null;
    }
    
</script>

<html>
<head>
    <title>ThaiCreate.Com Tutorial</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Panel id="pnlLogin" runat="server">
			<asp:Label id="lblLogin" Text="Login." runat="server" />
			<br />
			<br />
				<table border="1" style="width: 300px">
                <tbody>
                    <tr>
                        <td>
                            &nbsp;<asp:Label id="lblUsername" runat="server" text="Username"></asp:Label></td>
                        <td>
                            &nbsp;<asp:TextBox id="txtUsername" runat="server" Width="120px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="ChkUsername" runat="server" 
                                ControlToValidate="txtUsername" ErrorMessage="* Username"></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            &nbsp;<asp:Label id="lblPassword" runat="server" text="Password"></asp:Label></td>
                        <td>
                            &nbsp;<asp:TextBox id="txtPassword" runat="server" Width="120px" 
                                TextMode="Password"></asp:TextBox>
                        </td>
                    </tr>               
                </tbody>
            </table>
            <br />
            <asp:Button id="btnLogin" onclick="btnLogin_Click" runat="server" Text="Login"></asp:Button>
            <br />
        </asp:Panel>
        <asp:Label id="lblStatus" runat="server" visible="False"></asp:Label>
    </form>
</body>
</html>


Logout.aspx

<%@ Page Language="C#" %>
<script runat="server">
	public void Page_Load(object sender, EventArgs e)
	{
		Session.Abandon();
        Response.Redirect("Login.aspx");
	}
</script>
<html>
<head>
    <title>ThaiCreate.Com Tutorial</title>
</head>
<body>
    <form id="form1" runat="server">
        
    </form>
</body>
</html>


Page1.aspx

<%@ Page Language="C#" %>
<script runat="server">

	public void Page_Load(object sender, EventArgs e)
	{
		if (Session["strUser"]==null) {
            Response.Redirect("Login.aspx");
			Response.End();
		} else {
			this.lblStatus.Text = "Welcome [" + Session["strUser"] + "]";
		}
	}

</script>
<html>
<head>
    <title>ThaiCreate.Com Tutorial</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label id="lblStatus" runat="server"></asp:Label>
		<br />
		<br />
		<asp:HyperLink id="hplLogout" NavigateURL="Logout.aspx" Text="Logout" runat="server"></asp:HyperLink>
    </form>
</body>
</html>


Web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
    <appSettings>
      <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
    </appSettings>
</configuration>


Screenshot

method 1 tum

method 2 tum







   
Share
Bookmark.   

  By : doanga1
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2019-05-04
  Download : No files
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่