简体   繁体   English

ASP.NET:HttpContext.Current.User.Identity.Name似乎在page_load上随机失去其值

[英]ASP.NET:HttpContext.Current.User.Identity.Name seems to loosing its value randomly on page_load

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. 基本上,我在Default.aspx页上有一个登录控件,在该页上我正在对用户进行身份验证,并在有效的用户名和密码上将它们导航到另一个页面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. 在页面Upload.aspx上,我想将用户名值存储在全局变量中,然后在SQL过程中传递该全局变量。

To get the username value on page Upload.aspx I have this code under page_load event 要在Upload.aspx页面上获取用户名值,我在page_load事件下有此代码

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). Find_UserType_Of(uname)是clsClass中的一个方法,它采用字符串参数(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! Find_UserType_Of(uname)方法需要一个未传递的参数!

Any idea, what is going on? 任何想法,这是怎么回事?

Thanks 谢谢

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. 最大的问题是变量上的static变量,这将导致它的值更改,具体取决于页面上的内容,因为该变量已为所有请求共享。 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 在我的Ajax PageMethod访问页面级私有静态属性的响应中可以找到更多解释。

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 . 更新 :由于没有设置un的值,因此在回发上也有问题。 After postback, you still need to set un , or just use HttpContext.Current.User.Identity.Name . 回发后,您仍然需要设置un ,或者仅使用HttpContext.Current.User.Identity.Name

Every request to an ASP.NET application creates a new instance of the System.Web.UI.Page , in this case Upload . 对ASP.NET应用程序的每个请求都会创建System.Web.UI.Page新实例 ,在本例中为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. 您可以通过使用ViewState使您的网页表现得像状态一样来解决此限制。

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 . 另外,对于全局变量,请使用Application字典: Application["uname"] = HttpContext.Current.User.Identity.Name对于用户,请使用Session字典Session["uname"] = HttpContext.Current.Identity.Name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.net:IIS 7.5中的HttpContext.Current.User.Identity.Name为空,但在IIS Express上不是 - ASP.net: HttpContext.Current.User.Identity.Name is empty in IIS 7.5 but not on IIS Express 模拟HttpContext.Current.User.Identity.Name - Mock HttpContext.Current.User.Identity.Name 更改HttpContext.Current.User.Identity.Name - Changing HttpContext.Current.User.Identity.Name @HttpContext.Current.User.Identity.Name 返回不同的用户名 - @HttpContext.Current.User.Identity.Name returns a different user name 用户登录后更改HttpContext.Current.User.Identity.Name - Changing HttpContext.Current.User.Identity.Name after User is logged in HttpContext.Current.User.Identity.Name始终为null - HttpContext.Current.User.Identity.Name is always null HttpContext.Current.User.Identity.Name返回DefaultAppPool,为什么? - HttpContext.Current.User.Identity.Name returns DefaultAppPool, why? 访问DbContext中的HttpContext.Current.User.Identity.Name - Accessing HttpContext.Current.User.Identity.Name in the DbContext HttpContext.Current.User.Identity.Name在.ashx处理程序中不起作用 - HttpContext.Current.User.Identity.Name not working in .ashx hander HttpContext.Current.User.Identity.Name无法转换 - HttpContext.Current.User.Identity.Name can't be converted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM