Membuat Login dengan asp.net

Standar

1. pada webconfig

<connectionStrings>

<add connectionString=”Data Source=  ;Initial Catalog= ;User= ;Password= ” providerName=”System.Data.SqlClient”/>

</connectionStrings>

isi sesuai dengan datasourcenya

2. add new item (klik kanan – add new item – pilih web form lalu ketik name : login.asp)

membuat table login pada login.aspx tambahkan :

<asp:Label ID=”Label1″ runat=”server” Text=””></asp:Label>

<table>

 <tr>

<td>Username</td>

<td>

<asp:TextBox ID=”txtuser” runat=”server”></asp:TextBox></td>

</tr>

<tr>

<td>Password</td>

<td>

<asp:TextBox ID=”txtpass” TextMode=”Password” runat=”server”></asp:TextBox></td>

</tr>

<tr>

<td></td>

<td>

<asp:Button ID=”Button1″ runat=”server” Text=”Ok” onclick=”Button1_Click” />

<asp:Button ID=”Button2″ runat=”server” Text=”Cancel” />

</td>

</tr>

</table>

isikan source codenya pada login.aspx.cs :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.IO;

using System.Configuration;

using System.Web.Security;

using System.Data;

public partial class login : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button1_Click(object sender, EventArgs e)

{

SqlConnection coninv = new SqlConnection(ConfigurationManager.ConnectionStrings[“coninv”].ConnectionString);

try

{

coninv.Open();

SqlCommand cmd = new SqlCommand(“select * from tb_user where nik='” + txtuser.Text + “‘ and user_pass='” + txtpass.Text + “‘ and status=’0′”, coninv);

SqlDataReader rdr = cmd.ExecuteReader();

if (rdr.Read())

{

 

FormsAuthenticationTicket authTicket =

new FormsAuthenticationTicket(1, // version

txtuser.Text + “-” + rdr[“username”] + “-” + rdr[“user_priv”] + “-” + rdr[“id_user”] + “-” + rdr[“region”],

DateTime.Now,

DateTime.Now.AddMinutes(Session.Timeout), false, txtpass.Text); 

string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

 

HttpCookie authCookie =

new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

 

Response.Cookies.Add(authCookie);

Response.Redirect(FormsAuthentication.GetRedirectUrl(txtuser.Text, false));

}

else

{

Label1.Text = “username tdk dapat mengakses”;

}

}

catch (Exception ex)

{

if (coninv.State == ConnectionState.Open)

{

Label1.Text = ex.Message;

coninv.Close();

}

}

}

}

3. tambahkan item dengan nama home.asp

 home.aspx

<asp:Label ID=”Label1″ runat=”server” Text=””></asp:Label>

<asp:LinkButton ID=”LinkButton1″ runat=”server” OnClick=”log”>Logout</asp:LinkButton>

home.aspx.cs

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.IO;

using System.Configuration;

using System.Web.Security;

public partial class home : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Label1.Text = “sukses”;

}

protected void log(object sender, EventArgs e)

{

Response.Redirect(“logout.aspx”);

}

4. tambahkan item dengan nama : Logout.aspx

pada logout.aspx.cs

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.IO;

using System.Configuration;

using System.Web.Security;

public partial class logout : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Session.Abandon();

FormsAuthentication.SignOut();

Response.Redirect(“login.aspx”); 

}

}

5. Pada webconfig.aspx tambahkan

<authentication mode=”Forms”>

<forms defaultUrl=”default.aspx” loginUrl=”login.aspx” name=”apaaja” protection=”All”></forms>

</authentication>

<authorization>

<deny users=”?”/>

<allow users=”*”/>

</authorization>

6. Pada default.aspx tambahkan

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Redirect(“home.aspx”);

}

}

Satu tanggapan »

  1. kalo coding yang sederhananya gimana?
    yang usernya memiliki hak izin akses yang sama, kalo yg di atas kan tergantung level. tolong yah,,,

    • itu justru tidak menggunakan level user hanya login lalu di cek apakah username dan password yg diinput sama dan statusnya aktif (0), status aktif disini si user masih bisa login dan belum di delete,jika user tidak aktif/sudah di delete maka statusnya (1)
      query untuk cek
      select * from tb_user where nik=’” + txtuser.Text + “‘ and user_pass=’” + txtpass.Text + “‘ and status=’0′”

    • itu justru tidak menggunakan level, user hanya login lalu di cek apakah username dan password yg diinput sama dan statusnya aktif (0), status aktif disini si user masih bisa login dan belum di delete,jika user tidak aktif/sudah di delete maka statusnya (1)
      query untuk cek
      select * from tb_user where nik=’” + txtuser.Text + “‘ and user_pass=’” + txtpass.Text + “‘ and status=’0′”

  2. kalo hasilnya kaya gini salah dmnanya???

    The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:

    1. Add a “Debug=true” directive at the top of the file that generated the error. Example:

    or:

    2) Add the following section to the configuration file of your application:

      • kalo kya gni gmna???

        Line 18: protected void Button1_Click(object sender, EventArgs e)
        Line 19: {
        Line 20: SqlConnection coninv = new SqlConnection(ConfigurationManager.ConnectionStrings[“coninv”].ConnectionString);
        Line 21: try
        Line 22: {

      • Mba Lilis,, yg kemarin udah bisa, tp kok pas login muncul pesan
        “Username tdk dapat mengakses”
        padahal user n passwordnya bener,,
        klo kya gtu salah dmna mba??? d tolong yah mba,,,

Tinggalkan Balasan ke idax Batalkan balasan