简体   繁体   中英

How to get a non-static value in a static “webmethod” function in asp.net-4.5

In my asp.net-4.5 solution. I have User.Username variable, which is set correctly but it is non-static. I need to use this variable in a static method, which is called via AJAX in my .aspx page, below:

    [WebMethod]
    public static bool DeleteFromDB (string programId)
    { 
       ... 
       Log.Write("deleted by: " + User.Username)        
    }

My problem is I cannot use User.Username there since it is a non-static variable. I could not manage to create a static variable and set its value to static one, or convert non-static to static in a way. Tried to using a hiddenField and Label, but can't reach their values from static function as well.

Note that User.Username is not equal to System.Security.Principal.WindowsIdentity.GetCurrent().Name;

User.Username is the email address that the user uses while logging in.

What is the easiest way to succeed it? Thanks.

You must ensure that when a user logs in a data which uniquely identifies him/her is stored as session data (id, username or email can be that data, but id is to be preferred). This is the first thing to do if it is not already done.

You should be able to get your user by session data and if you get that into a variable, let's call it myUser , then you will be able to read its Username , preferably via a getter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM