0

How could I get a default depending if the user is logged in or not?

public class ShippingDetails {

  public ShippingDetails() {                
    if (HttpContext.Current.User.Identity.Name != "") {
        Name = "";                
    }             
   }

   public string Name { get; set; }
}

2 Answers 2

2

You mean get the users login name, otherwise just give them a dummy name? Check if they're logged in using Request.IsAuthenticated. If they are, grab the username, if not just set it.

if (HttpContext.Current.Request.IsAuthenticated)
    Name = HttpContext.Current.User.Identity.Name;
else
    Name = "User";
Sign up to request clarification or add additional context in comments.

2 Comments

cool. seems like this works too. Wonder if there's a benefit in using your method over mine, or vice versa
@Shredder, Not sure, but I prefer to use Request.IsAuthenticated because it makes it clear what I'm checking.
0

You can also check

if (HttpContext.Current.User.Identity.IsAuthenticated) {
   Name = HttpContext.Current.User.Identity.Name;
} 
else 
{ 
   Name = "Anonymous"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.