Asp.net mvc 4 Comparing HttpContext.User.Identity.Name

Asp.net mvc 4 Comparing HttpContext.User.Identity.Name,asp.net-mvc-4,Asp.net Mvc 4,I am trying this: string username = httpContext.User.Identity.Name.ToString(); if (u.Equals(username)) { authorise = true; } the domain name and the username are exactly the same, letter for letter, backslash for backslash, capitals for capitals etc.. but the variable authorise is still false. Not sure why. I am beginning to wonder whether there is a way that is considered better practice for doing this.

I am trying this:

string username = httpContext.User.Identity.Name.ToString();
if (u.Equals(username))
{
    authorise = true;
}

the domain name and the username are exactly the same, letter for letter, backslash for backslash, capitals for capitals etc.. but the variable authorise is still false. Not sure why. I am beginning to wonder whether there is a way that is considered better practice for doing this.


#1

This maybe a mismatch of some kind. I would write a unit test to verify your assumptions here.

The backslashes may be escaped differently. You could try matching without these.

function string CleanAndTrim(string user){
    var cleaned = user.Replace(@"\", "");
    cleaned = user.Replace(@"/", "")
    return cleaned.ToLower().Trim();
}

if(CleanAndTrim(u)==CleanAndTrim(username){
  authorise = true;
}

#2

Why are you calling .ToString() on a string? And why are you using an if-statement to set a boolean value? -- Assign the boolean directly. Try this:

string username = httpContext.User.Identity.Name;
authorise = u.Equals(username);

I wouldn't worry about best practices right now, as you need to get the basics down first.


#3

what's the question? you say they match and it's being set to true, that's what I'd expect.

#4

@dove they match, but it is not set to true

#5

okay, your question says otherwise

#6

have you access to a testing framework?