0
votes

In Global.asax i have:

void Application_Start(object sender, EventArgs e) 
{

    //Key is Login name and value is password
    Application["dan"] = "123";
    Application["rafi"] = "123";
    Application["nick"] = "123";
    Application["dave"] = "123";


}

Now i have login.aspx page where i need to check login and password that is in application state. I have two textboxes and button. I need to check if value typed in texboxUserName.text exists in application state, values needed to be -dan, rafi, nick or dave. how do i do that?:) It's an exercise in our school..

UPD Ok i did it:) thx for help

protected void btnLogin_Click(object sender, EventArgs e) {

    string name=txtUserName.Text;
    string pass = txtPassword.Text;




    foreach (string key in Application.AllKeys)
    {
        if (key == name)
        {
            string curectName = name;

            if ((string) Application[curectName] == pass)
            {
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                lblError.Text = "pass don't match";
            }

        }

        else
        {
            lblError.Text = "no name";
        }
    }
4
Weird way of authentication, why like that?Paweł Smejda
So if someone types in the name of some other application state variable along with the expected value of that variable, then they are authenticated? This is terrible security! I hope this class isn't trying to teach security, unless this is given as a counter-example. ;)Jeffrey L Whitledge
No :) It's not.. We learn application state, best example of application state is "click counter", but our teacher wanted us to do something different.RaShe

4 Answers

1
votes

Similar as you add it:

if(!String.IsNullOrEmpty(Application[texboxUserName.Text]))
{
    string password = Application[texboxUserName.Text].ToString();
}
0
votes

If the entry in Application does not exist, the value will be Null. You can do it on one line, but for clarity, here's a complicated example:

if(Application[strUserName] != Null){
  // Got username entry
  if(Application[strUserName] == strPassword){
     // Do something
  }
}

That said, it's generally not a good idea to keep usernames and passwords in application state.

0
votes

I think you can try:

if(Application[texboxUserName.text] != null)
{
   if(Application[texboxUserName.text] == textboxPassword) 
   {
        return true;
   }
   else
   {
        return false;
   }
}

i am writing from head, i didn try this code.

texboxUserName.text and textboxPassword are values of textboxes.

0
votes
    if (Application[textboxUserName.Text] != null 
        && Application[textboxUserName.text] == textboxPassword.Text)
    {
       // username + password exist
    }