Question

I have some code from the 4guysfromrolla website which gives a tutorial on logging in as another user with admin credentials.

I have most of it working but i'm having some trouble translating this part of the code from VB to C#. The part I'm having trouble translating is the first if statement.

If Page.User.Identity IsNot Nothing AndAlso TypeOf Page.User.Identity Is FormsIdentity Then
   Dim ident As FormsIdentity = CType(Page.User.Identity, FormsIdentity)
   Dim ticket As FormsAuthenticationTicket = ident.Ticket
   Dim AdminUserName As String = ticket.UserData

   If Not String.IsNullOrEmpty(AdminUserName) Then
      'An Admin user is logged on as another user... 
      'The variable AdminUserName returns the Admin user's name
      'To get the currently logged on user's name, use Page.User.Identity.Name
   Else
      'The user logged on directly (the typical scenario)
   End If
End If

I would be most grateful if someone could help me translate this! This is the part where the page detects if the user is indeed an admin logging in as another user, so that I can programmatically display a panel with a reminder of that.

Was it helpful?

Solution

From http://converter.telerik.com/:

if (Page.User.Identity != null && Page.User.Identity is FormsIdentity) {
    FormsIdentity ident = (FormsIdentity)Page.User.Identity;
    FormsAuthenticationTicket ticket = ident.Ticket;
    string AdminUserName = ticket.UserData;

    if (!string.IsNullOrEmpty(AdminUserName)) {
    //An Admin user is logged on as another user... 
    //The variable AdminUserName returns the Admin user's name
    //To get the currently logged on user's name, use Page.User.Identity.Name
    } else {
        //The user logged on directly (the typical scenario)
    }
}

OTHER TIPS

if (Page.User.Identity != null && Page.User.Identity is FormsIdentity)
{
    ....
}

I assume you are OK with the rest.

If Page.User.Identity IsNot Nothing AndAlso TypeOf Page.User.Identity Is FormsIdentity Then

Would be

if( Page.User.Identity != null && Page.User.Identity is FormsIdentity )

AndAlso is only an AND operator that only evaluate the left side if the left side is false (Default behavior in C#).

To check if a object is nothing, compare with null, and to check if an object is of a type, use the is operator.

How about

if (Page.User.Identity != null && Page.User.Identity is FormsIdentity)
{
   var ident = (FormsIdentity)Page.User.Identity;
   var ticket  = ident.Ticket;
   var AdminUserName = ticket.UserData;

   if (!String.IsNullOrEmpty(AdminUserName))
   {
      'An Admin user is logged on as another user... 
      'The variable AdminUserName returns the Admin user's name
      'To get the currently logged on user's name, use Page.User.Identity.Name
   }
   else
   {
      'The user logged on directly (the typical scenario)
   }
}

Stackoverflow is not a translation service, however...

var fIdent = User.Identity as FormsIdentity;
if(fIdent != null)
{
    string adminUserName = fIdent.Ticket.UserData;
    if(!String.IsNullOrEmpty(adminUserName))
    {
        // an Admin user is logged on as another user... 
    }
    else
    {
        // the user logged on directly (the typical scenario)
    }
}

What you are asking us to do is essentially teach C#.

As I mentioned initially in my comment, use a convertor if you are unsure. After that however, compare the differences in your VB.NET code to your C# and see the structural differences.

VB.NET

If True Then
  'Do Stuff
End If

C#

if(true){
 //Do stuff
}

Differences of the above, condtion is wrapped in () and then and end if are replaced with parenthesis. You shouldn't just take the code straight out, read it and then compare it. Then try and re-write it yourself :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top