Click here to Skip to main content
15,888,205 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All

I am trying to give a user based authorisation to certain urls.
I have configured the web.config at the root as below

<authentication mode="Forms">
      <forms
          name="MyAuth"
          loginUrl="ABC/Login.aspx"
          protection="All"
          path="/"
        />
    </authentication>


another web.config at the ABC Directory as below

<?xml version="1.0"?>
<configuration>
    <system.web>
      <authorization>
        <deny users="?" />
      </authorization>
    </system.web>
</configuration>


every thing is working fine except the login
when I access the directory ABC login page is displayed even after giving correct username and password the page is redirected to login page itself.

I am new to C# and ASP.net

Please help me
my Code at aspx.cs is as below

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
           
            string selectString = "SELECT * FROM users " + "WHERE Username = '" + Login1.UserName + "' AND Password = '" + Login1.Password + "'";

            MySqlCommand mySqlCommand = new MySqlCommand(selectString,con);
            con.Open();
            String strResult = String.Empty;
            strResult = mySqlCommand.ExecuteScalar().ToString();
            con.Close();

            if (strResult.Length > 0)
            {
                e.Authenticated = true;
                Response.Redirect("up.aspx");
            }

            else
            {
                MsgBox("Wrong username or password!.", this.Page, this);
                return;
            }
        }


Please help me
Thank You
Santosh Sharma
Posted

1 solution

I think you need to create a forms authentication ticket object, encrypt and store the same in cookie before redirecting to authenticated pages.

C#
FormsAuthentication.SignOut();
Session.RemoveAll();

// Create forms authentication ticket
var ticket = new FormsAuthenticationTicket(
1, // Ticket version
txtMemUserName.Text.Trim(),// Username to be associated with this ticket
DateTime.Now, // Date/time ticket was issued
DateTime.Now.AddMinutes(2880), // Date and time the cookie will expire
false, // if user has checked remember me then create persistent cookie
"mem", // store the user data, in this case roles of the user
FormsAuthentication.FormsCookiePath); // Cookie path specified in the web.config file in <Forms> tag if any.

// To give more security it is suggested to hash it
var hashCookies = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies); // Hashed ticket

// Add the cookie to the response, user browser
Response.Cookies.Add(cookie);
 
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