0

Basically, I have a login control on Default.aspx page, where I am authenticating a user and on valid username and password, I am navigating them to another page Upload.aspx.

On page Upload.aspx, I want to store username value in a global variable and then pass this global variable around in number of SQL procedure.

To get the username value on page Upload.aspx I have this code under page_load event

public partial class Upload : System.Web.UI.Page 
{   
    public static string uname;
    public static string un;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                un = HttpContext.Current.User.Identity.Name;
                uname = un;

                clsClass find_user_type = new clsClass();
                user_Type = find_user_type.Find_UserType_Of(uname).Trim();
            }
            else
            {
                uname = un;  //the first If condition will be true atleast 1st, 
                             //so that one `un` is set i can copy it into `uname` 
                             //when its postback
            }
        }
}

Find_UserType_Of(uname) is a method in clsClass that takes a string parameter (uname).

Now, this code runs just fine when I am running it on local machine when server is my localhost. But when I upload this to a webserver, it starts acting funny and tells me that a procedure in

Find_UserType_Of(uname) method requires a parameter which was not passed!

Any idea, what is going on?

Thanks

user1889838
  • 325
  • 4
  • 13
  • 37

2 Answers2

4

Your biggest problem is the static on your variables, which will cause it's value to change, depending on who is on the page, since that variable is shared for all requests. You didn't notice locally since you were the only person making requests:

public static string uname;
public static string un;

which should instead be

private string uname;
private string un;

More explanation can be found in my response at Ajax PageMethod accessing page-level private static property

You may also want to read this on private variables and also related: Why should I use a private variable in a property accessor?

UPDATE: You also have a problem on Postback, because you are not setting the value of un. After postback, you still need to set un, or just use HttpContext.Current.User.Identity.Name.

Community
  • 1
  • 1
MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
  • All I want to do is to save logged in user's username into a global variable. Are there any other approaches? – user1889838 Jan 08 '13 at 18:41
  • 1
    You already have access to the logged in user's name using HttpContext.Current.User.Identity.Name. Why can't you just use that? – MikeSmithDev Jan 08 '13 at 18:42
  • @user1889838 updated answer... there was another issue where your variables are not set on postback. – MikeSmithDev Jan 08 '13 at 18:45
  • I am frustrated because on every page load I check for `HttpContext.Current.User.Identity.IsAuthenticated` value, and if this is false, I take them to login page. Now, it randomly loosing its value on this page_load event of `Upload.aspx` hence user ends up landing on login page. There are many controls who have autopostback = true on Upload.aspx page. So it makes sense to check the users authentication value. – user1889838 Jan 09 '13 at 12:39
0

Every request to an ASP.NET application creates a new instance of the System.Web.UI.Page, in this case Upload. That means every variable must be set on every request which will be using it. You can work around this limitation by using ViewState to make your web page behave like it has state.

Alternatively, for global variables use the Application dictionary: Application["uname"] = HttpContext.Current.User.Identity.Name or for users, use the Session dictionary Session["uname"] = HttpContext.Current.Identity.Name.

just.another.programmer
  • 8,579
  • 8
  • 51
  • 90