Click here to Skip to main content
15,922,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

In my web site I'm working on login for simple users who wants just to buy products and the administartors which they have other abilities .

I setup the account for administrator at ASP.NET configuration and the rules.

I create a folder Adminpages which contains the Admin.aspx and a web.config file which it have
XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <authorization>
            <allow users="Administrator" />
          <deny users="?"/>
        </authorization>
    </system.web>
</configuration>


In login page I create the following code:
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        string adm = "admin";
        string auth=Convert.ToString(Login1.UserName.ToUpper());
       
        if (auth == adm)
        {
            Response.Redirect("~/AdminPages/Admin.aspx");
        }
        else
        {
            //authentication code
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());

            string aSQL = "select ID_USER, NAME_USER, PASSWORD from [User] where UPPER(NAME_USER)= @USER and UPPER(PASSWORD)=@PASS";
            try
            {
                SqlCommand cmd = new SqlCommand(aSQL, con);
                cmd.Parameters.Add("@User", SqlDbType.Char, 10, "UserName").Value = Login1.UserName.ToUpper();
                cmd.Parameters.Add("@Pass", SqlDbType.Char, 10, "PASSWORD").Value = Login1.Password.ToUpper();
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                //check database for names
                dr.Read();
                if (dr.HasRows)
                {
                    Session["user"] = dr["ID_USER"];
                    Response.Redirect("cart.aspx?ID=" + Request.QueryString["ID"] + "&quant=" + Request.QueryString["quant"]);
                }
                else
                    Response.Write("User or password invalid");

            }
            finally
            {
                con.Close();
            }
        }

When I try to enter in shopping cart everything is ok but when i try to login as administrator
nothing happens.

Any idea what i must change in order to work the login in administrator page?

Thnx in advance!
Jason
Posted
Updated 15-Oct-13 9:17am
v3
Comments
Richard C Bishop 15-Oct-13 15:18pm    
Have you set up membership and roles in the WSAT?
JasonTsoum77 15-Oct-13 15:28pm    
Yes I have set,

thnx for your response
Richard C Bishop 15-Oct-13 15:36pm    
Well, you have "Administrator" as your user in the web.config, but in the code-behind you call it "admin". They need to be the same I believe.
JasonTsoum77 15-Oct-13 15:38pm    
How i should modify the web.config?
Richard C Bishop 15-Oct-13 15:38pm    
I am not sure which one you need to change. Whatever the WSAT says is the users name is what both need to say.

1 solution

See this article for examples on how to set up your code:

ccilk[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900