0

I am so confused as to why this is happenning in my application. I have an object I am getting from Session storage however in some cases it may not exist so I am doing some ternary checks. Even with the ternary checks I am erroring out with a Null Reference Exception on userInfo. The other weird part is that when I inspect firstName and lastName they both show "???".

@{ 
    // Session storage for user info
    var userInfo = Session["UserInfo"] as UserInfo;

    var firstName = userInfo != null ? userInfo.FirstName : "???";
    var lastName = userInfo != null ? userInfo.LastName : "???";
}

<div>
    @firstName @lastName // Errors here with object reference not set to an instance of an object. 'userInfo' was null
</div>

Am I crazy? I swear this is how I could check against null problems.

tokyo0709
  • 1,897
  • 4
  • 28
  • 49
  • In my experience when Visual Studio identifies a line in Razor code throwing an exception, the exception was *actually* thrown above it. –  Aug 09 '17 at 18:23
  • Not sure if that works or not, but ternary checks make code so much more complicated to read, understand, and maintain. Var has the same effect. Please use [strong typing](https://en.wikipedia.org/wiki/Strong_and_weak_typing). – SneakyTactician Aug 09 '17 at 18:23
  • Any reason why you're doing this in the View? – Grizzly Aug 09 '17 at 18:24
  • 8
    @SneakyTactician That is irrelevant to the question and disputable. –  Aug 09 '17 at 18:25
  • 2
    Side note, If you are targeting .NET 4.6 or above you can use the [null conditional operator](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operators). That would look like: `var firstName = userInfo?.FirstName ?? "???";` – maccettura Aug 09 '17 at 18:29
  • @Amy I know it is irrelevant to the question, it was meant to be a tip. Take it or leave it, that is for the OP to decide. – SneakyTactician Aug 09 '17 at 18:29
  • 3
    @SneakyTactician Using the conditional operator, or `var`, loses no static type checking over alternatives. You might not like it aesthetically, but claiming it results in different type checking is just false. – Servy Aug 09 '17 at 18:31
  • 1
    Have you tried shutting down Visual Studio and opening it back up again? I've noticed the Razor pages having issues compiling valid C# code before, and that usually fixes it. – krillgar Aug 09 '17 at 18:33
  • @Servy "Not sure if that works or not, but ternary checks make code so much more complicated to read, understand, and maintain. Var has the same effect. Please use strong typing." I never said it loses static type checking, only that it makes code more complicated to read, understand, and maintain. – SneakyTactician Aug 09 '17 at 18:34
  • 2
    @SneakyTactician So you didn't say, "Please use strong typing."? If you *had* only said that you personally don't like to read that code, and have difficulty understanding and maintaining it, then that would of course have been different (still *irrelivant*, but at least not incorrect). – Servy Aug 09 '17 at 18:35
  • 2
    @SneakyTactician "please use strong typing". He **is** using strong typing . `var` does not change that. –  Aug 09 '17 at 18:35
  • 3
    @SneakyTactician Not sure why you think using `var` means its week typed... You might want to re-read some documentation. – maccettura Aug 09 '17 at 18:35
  • Nevermind, I apologize I tried using `var` and I also did not receive NullReferenceExceptions – Grizzly Aug 09 '17 at 18:39
  • Hm, I did reload Visual Studio and all of a sudden it was compiling correctly. That is so weird. I think I will probably be upgrading to 4.6 or above to utilize the null conditional operator. Thanks to everyone trying to help solve this one. – tokyo0709 Aug 09 '17 at 18:44
  • 2
    @tokyo0709 it seems likely that you were using an old version of the razor page, and it just needed a good swift kick. –  Aug 09 '17 at 18:46
  • @Amy That is the feeling I am getting right now too. I was just staring at the page saying to myself like a crazy person, "There should be no exception!" – tokyo0709 Aug 09 '17 at 18:48
  • @tokyo0709 Why do you store user information in Session State? 15 years ago before ASP.NET Membership, we all stored user information in Session state. Ideally, you should be storing them in ***Principle*** object. – Win Aug 09 '17 at 20:03
  • @Win That is a very good question. Maybe you can help me solve this. I've developed several small scale apps in MVC but this is my first user based identity application. I want to be able to access user information wherever they are at in the application. Do I need principle to accomplish this? – tokyo0709 Aug 10 '17 at 05:51

1 Answers1

0

Comment: I've developed several small scale apps in MVC but this is my first user based identity application. I want to be able to access user information wherever they are at in the application. Do I need principle to accomplish this?

First of all, we should not store user information in Session to check whether user is authenticated or not. It is very fragile, and it cannot use ASP.NET MVC's build-in Authorize attribute.

Ideally, you want to use ASP.NET Identity. If you are new to ASP.NET Identity, you might want to watch free ASP.NET MVC 5 Fundamentals course by Scott Allen.

If you already have an existing user database, and do not want to use ASP.NET Identity, you could just use Owin Authentication Middleware. You can read more at this SO answer, and here is the working sample code.

Win
  • 61,100
  • 13
  • 102
  • 181
  • So I actually am utilizing Identity in the application. I've got the user flow setup fine and Authorize attributes for my controllers and actions. My main problem is that I want to be able to access the corresponding AspNetUser record for the current logged in user on quite a few views. (I've built in some custom relations that I want accessible to display information) I guess thats why I tried going down the session route. Should I be doing that differently? – tokyo0709 Aug 10 '17 at 19:08