Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does User.Identity.Name ever throw?

I notice User.Identity.Name appears to return the empty string when there is no user authenticated, rather than throwing a null reference exception.

It is valid to assume User.Identity.Name will never throw? Or should I be checking for null Identity before access.

like image 850
fearofawhackplanet Avatar asked May 08 '26 18:05

fearofawhackplanet


1 Answers

I believe that the User object can be null if you haven't called

FormsAuthentication.SetAuthCookie(UserName, False)

or

FormsAuthentication.RedirectFromLoginPage(UserName, False)

It would probably be best to wrap in a try catch.

like image 118
Andrew Walters Avatar answered May 10 '26 14:05

Andrew Walters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!