|
|
|
ต้องการให้ Login และกำหนดสิทธิ์ ต้องทำยังไงครับ asp.net (c#) |
|
|
|
|
|
|
|
คุณก็เขียนตรวจสอบ Session ไว้ครับ ในหน้า Default.aspx ครับ
Code (C#)
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"] + "]";
}
}
Go to : (C#) ASP.NET User Authentication/Login Form
|
|
|
|
|
Date :
2012-02-24 06:29:30 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณมากครับ
ผมขอถามเพิ่มนิดนึงครับ สมมุติผมมี page 10 หน้า แต่จะให้ user ดูได้แค่หน้าเดียว จะกำหนดสิทธิ์ยังไงครับ
|
|
|
|
|
Date :
2012-02-25 21:42:29 |
By :
iron |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใช่ครับ แต่คุณสามารถออกแบบให้เป็นแบบ User Control หรือ Master Page ได้ครับ
|
|
|
|
|
Date :
2012-02-25 22:53:16 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผมลองเพิ่มโค้ดเช็คเป็นดังนี้ โดย id ไม่ใช้ test ให้ส่งไปอีกหน้านึง
Code (C#)
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string username = Login1.UserName;
string pwd = Login1.Password;
string strConn;
strConn = WebConfigurationManager.ConnectionStrings["NUTAILOR"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConn);
Conn.Open();
string sqlUserName;
sqlUserName = "SELECT username,password FROM password";
sqlUserName += " WHERE (username = '" + username + "')";
sqlUserName += " AND (password ='" + pwd + "')";
SqlCommand com = new SqlCommand(sqlUserName, Conn);
string CurrentName;
CurrentName = (string)com.ExecuteScalar();
if (CurrentName != null && username == "test")
{
Session["UserAuthentication"] = username;
Session.Timeout = 1;
Response.Redirect("Default.aspx");
}
else if (CurrentName != null && username != "test")
{
Session["UserAuthentication"] = username;
Session.Timeout = 1;
Server.Transfer("employee.aspx");
}
else
{
Session["UserAuthentication"] = "";
}
}
}
แล้วผมใช้โค้ดดักที่ให้อื่นดังนี้
Code (C#)
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserAuthentication"] != "test")
{
Response.Redirect("employee.aspx");
Response.End();
}
}
ใน master page ผมใส่โค้ดดังนี้
Code (C#)
if (Session["UserAuthentication"] == null)
{
Response.Redirect("Login.aspx");
Response.End();
}
ผมเอาโค้ดที่ดัก user ไว้ใน master page ไม่ได้มันจะ error ส่งไปหน้านั้นไม่ได้ แสดงว่าผมต้องเข้าโค้ดดัก user ไปแปะทุกหน้าเลยครับ พอจะมีวิธีอื่นในการทำรึเปล่าครับ
|
|
|
|
|
Date :
2012-02-27 12:36:03 |
By :
iron |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ค่า "" กับ null มันต่างกันน่ะครับ ลองใช้
Code (C#)
if(Session["UserAuthentication"] == null or Session["UserAuthentication"] == "")
{
}
|
|
|
|
|
Date :
2012-02-27 21:36:58 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
/* for loop execution */
for (int a = 10; a < 20; a = a + 1)
{
Console.WriteLine("value of a: {0}", a);
}
Console.ReadLine();
}
Loop ครับ
|
|
|
|
|
Date :
2017-02-20 20:05:20 |
By :
as76157432 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|