Jorge Y. C. Rodriguez
Jorge Y. C. Rodriguez

Reputation: 3449

Fetching data to make default values in c#?

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; }
}

Upvotes: 0

Views: 80

Answers (2)

Massimo Zerbini
Massimo Zerbini

Reputation: 3191

You can also check

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

Upvotes: 0

Brandon
Brandon

Reputation: 69983

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";

Upvotes: 2

Related Questions