jeudi 17 avril 2014

ASP.net - c# rôle fondé login - Stack Overflow


I am working on a project that i want to use role based login. Ite a really simple project and not for external use at all. Its just a proof of concept. What i had thought about was using a page redirect to an admin page in the page load event. I know this is kinda not the most efficient coding. This redirect would be based upon critera in the DB. I have set a field to "Admin" and if this condition is met then it would redirect to an admin page. So i tried the code below and it doenst work correctly. Any help/input would be appreciated.


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

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


if (Session["New"] != null)
{
lblWelcom.Text += Session["New"].ToString();

if (Session["New"] != null)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ShowUsersConnectionString"].ConnectionString);
conn.Open();
string checkAdmin = "select count(*) from Staff where Admin='" + "Admin" + "'";
SqlCommand com = new SqlCommand(checkAdmin, conn);


Response.Redirect("Register.aspx"); //this is the admin page


conn.Close();
}
else
{
Response.Redirect("profile.aspx"); //this is the non admin page
}

}
else

Response.Redirect("Login.aspx");



}
protected void btnLogout_Click(object sender, EventArgs e)
{
Session["New"] = null;
Response.Redirect("Login.aspx");
}
}



Try This:


int RowsCount = Convert.ToInt32(com.ExecuteScalar());
if(RowsCount > 0)
Response.Redirect("Register.aspx");

Suggestion : You need to change your table column name from Admin to UserRole or something which is more readable.



I am working on a project that i want to use role based login. Ite a really simple project and not for external use at all. Its just a proof of concept. What i had thought about was using a page redirect to an admin page in the page load event. I know this is kinda not the most efficient coding. This redirect would be based upon critera in the DB. I have set a field to "Admin" and if this condition is met then it would redirect to an admin page. So i tried the code below and it doenst work correctly. Any help/input would be appreciated.


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

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


if (Session["New"] != null)
{
lblWelcom.Text += Session["New"].ToString();

if (Session["New"] != null)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ShowUsersConnectionString"].ConnectionString);
conn.Open();
string checkAdmin = "select count(*) from Staff where Admin='" + "Admin" + "'";
SqlCommand com = new SqlCommand(checkAdmin, conn);


Response.Redirect("Register.aspx"); //this is the admin page


conn.Close();
}
else
{
Response.Redirect("profile.aspx"); //this is the non admin page
}

}
else

Response.Redirect("Login.aspx");



}
protected void btnLogout_Click(object sender, EventArgs e)
{
Session["New"] = null;
Response.Redirect("Login.aspx");
}
}


Try This:


int RowsCount = Convert.ToInt32(com.ExecuteScalar());
if(RowsCount > 0)
Response.Redirect("Register.aspx");

Suggestion : You need to change your table column name from Admin to UserRole or something which is more readable.


0 commentaires:

Enregistrer un commentaire