I have application page, hosted on Sharepoint server (for ex. http://myportal/mysite/_layouts/application/default.aspx), which has code like this:
protected void Page_PreLoad(object sender, EventArgs e)
{
var userEmail = SPContext.Current.Web.CurrentUser.Email;
}
If user tries to get this page directly by URL after browser starts, exception appears, because CurrentUser is null. But if user firstly navigates to web site (http://myportal/mysite) and then to application page, CurrentUser is not null. So, how can I get CurrentUser object if it is not initialized in SPContext?
Getting the current user from an elevated SPWeb inside a RunWithElevatedPrivileges code. Try this code.