User.Identity.Name always returning false

Posted by mohammedr88-18313 under ASP.NET MVC on 11/25/2013 | Points: 10 | Views : 1491 | Status : [Member] | Replies : 1
Hi all
the below function "User.Identity.Name" always returning false , but it have to return true , kindly check where i going wrong

public ViewResult Show(int id)
var entry = _db.Entries.Find(id);
bool hasPermission = User.Identity.Name == entry.Name;
ViewData["hasPermission"] = hasPermission;
return View(entry);


Posted by: Sheonarayan on: 11/25/2013 [Administrator] HonoraryPlatinum | Points: 25

I think this is not the right way to compare two strings. Probably there is the difference in the case (upper or lower case).

User following code

User.Identity.Name.Equals(entry.Name,  StringComparison.CurrentCultureIgnoreCase);
In this case it returns true if both strings are same even in different case otherwise false.

Also make sure that user is authenticated before comparing. To check whether user is authenticated, use below code snippet

Thanks. Hope this helps.

Sheo Narayan

mohammedr88-18313, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response