-1

I have 2 RAZOR Index forms in my application. 1 Form creates records in one database "LocalDB" and updates the same record in another database "Core DB". Another Form sync records from "CoreDB" to "LocalDB". Have to do this because there are other applications who creates records into "CoreDB". In my local db, I am keeping the audit trail by 4 columns. CreateAt, CreatedBy, ModifiedAt, ModifiedBy. These columns give me info about who created/modified record and at what time.

I have a simple static class with a static function where I am getting the current user loged into my web application as

currentUser = HttpContext.Current.User.Identity.Name;     

This works when i user Form 1 where i create records. When i use the Form 2, where trying to Sync record from COREDB to LocalDB, this same line throws null exception.

Can you please advice why this is happening?

Thanks!

Vivek Patel
  • 349
  • 2
  • 4
  • 21
  • 2
    You should really check `HttpContext.Current.User.IsAuthenticated` before you go querying the `Identity` object :) – Mathew Thompson May 29 '12 at 18:41
  • `HttpContext.User` has no `IsAuthenticated` method but `HttpContext.User.Identity.IsAuthenticated` does exist, there's no `.Current` on the `HttpContext` object – CD Smith May 29 '12 at 19:00
  • If you add System.Web as your header files then you could use HTTPContext.Current to get the currently logged in user. This is how I have been using. I cant use HTTPContext.User because the VS Intellicense does not have that option in my static class. This is not in Controller. This is in one of my header classes. – Vivek Patel May 29 '12 at 19:13
  • That's so weird! I don't get a `HttpContext.Current` in any class where I have `using System.Web;` something is clearly amiss with my environment :-) – CD Smith May 29 '12 at 19:41

2 Answers2

2

Check HttpContext.Current != null && HttpContext.Current.User.Identity != null && HttpContext.Current.User.Identity.IsAuthenticated before looking to HttpContext.Current.User.Identity.Name

robrich
  • 13,017
  • 7
  • 36
  • 63
  • I tried this but still it is not working. HttpContext currentContext = HttpContext.Current; string currentUserName = currentContext.User.Identity.Name; Not sure what is going on. The same works from every other page. Its just not this page. – Vivek Patel May 29 '12 at 20:49
  • Are you inside `Task.Run` or `Parallel.Each` or some other async expression that got you off the request's thread? – robrich May 30 '14 at 20:37
0

You haven't explained in your question where and how are you calling the method containing this line. You talked about syncing some databases. I very much suspect that you are attempting to use HttpContext.Current inside a background thread which obviously is impossible because there's no any HttpContext in background threads.

If this is not the case then make sure that the user is authenticated before accessing its name:

string currentUser = "";
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
    currentUser = HttpContext.Current.User.Identity.Name;
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928