already answered here:
Get current logged user name for user "Account operates as System"
also like to note:
public SPUser GetUserWithElevated()
SPUser user = null;
SPSecurity.CodeToRunElevated elevatedSubmit = new
user = SPContext.Current.Web.CurrentUser;
your running under the system account ;)
also like to note if the solution in my other answer doesnt work as i presume it doesnt... that could be because your account is running under a system account? when you log into sharepoint what user name is displayed?
under module this should work:
if that doesnt work than try the spcontext method:
as i said, goto the url on the browser to first make sure the account your using is not set as a system account. Runwithelevated priv uses the app pool account which also shows as system account! In the code you provided is that three methods you have tried that all give system accounts?
You need to test with a dummy account to make sure its not the account your currently using that is the issue
if your using the windows identity to login users into sharepoint than you can do:
string getCurrentWindowsUserName = WindowsIdentity.GetCurrent().Name;
returning different account types depends on your settings you have from web.config to code like you have above (elvated priv) to sharepoint central admin (setting account as system account):
ok so based on your answer below i can say that the code is working as its passing the correct value! the place its going wrong could be in two places!
1) goto central admin and check the webapplication user policy. Click on the user and see if the tickbox run as system account is checked on a user you know is not ment to be run as system account.
i have a strong feeling the above is not the issue as you can have local accounts created on sharepoint site that are also showing the same issue? so based on this presumption follow number 2..
2) goto iis manager -> goto the site in question (click on the site) -> on the right hand side click on Advanced options -> a popup will appear -> make sure "physical path credentials" is empty.... click on the button '...' and you should get a popup -> make sure you have application user is checked and click ok!
hopefully above should solve your issue!