2013-02-06 3 views
-1

으로 번역했습니다. 4guysfromrolla 웹 사이트의 일부 코드에서 관리자 자격 증명을 가진 다른 사용자로 로그인하는 방법에 대한 자습서를 제공합니다.vb 코드를 C#

저는이 코드의 대부분을 VB에서 C#으로 번역하는 데 어려움을 겪고 있습니다. 번역에 어려움을 겪고있는 부분은 처음으로 if입니다.

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 

누군가 내가 이것을 번역 할 수 있다면 매우 감사 할 것입니다. 이 부분은 사용자가 실제로 다른 사용자로 로그인하는 경우 페이지가 감지되는 부분이므로이를 미리 프로그래밍하여 패널을 표시 할 수 있습니다. http://converter.telerik.com/에서

+2

http://www.developerfusion.com/tools/convert/vb-to-csharp/ – LukeHennerley

+0

감사합니다 !! 나는 그것을 definately 히 북마크 할 것이다!! :) – Stuart

+0

문제는 없습니다. 'C#'에 대한 개발과 지식을 돕기위한 자세한 지침은 내 대답을 참조하십시오 :) – LukeHennerley

답변

1

는 :

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) 
    } 
} 
+0

대단히 감사합니다 !! – Stuart

+0

당신은 환영합니다! –

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

난 당신이 나머지 OK입니다 가정합니다.

+0

대단히 감사합니다 !! – Stuart

0

에 대한

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) 
    } 
} 
1
If Page.User.Identity IsNot Nothing AndAlso TypeOf Page.User.Identity Is FormsIdentity Then 

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

AndAlso

왼쪽은 (C#의 기본 동작은) false 경우에만 왼쪽을 평가 만 AND 연산자입니다 것이 얼마나.

개체가 아무것도 없는지 확인하고 null과 비교하고 개체가 형식인지 확인하려면 is 연산자를 사용하십시오.

0

유래는 무엇 당신이 우리에게 요구하는 것은 본질적으로 C#을 가르 칠

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) 
    } 
} 
+0

번역 서비스가 아니란 점을 고맙게 생각합니다. 어디로 가야할지 모르겠지만 나중에 참조 할 수 있도록 여기에 게시 된 변환기를 표시된 책을 가지고 있습니다. – Stuart

0

... 그러나, 번역 서비스가 아닙니다.

내가 처음 언급 한 내용에 확신이없는 경우 변환기를 사용하십시오. 그런 다음 VB.NET 코드의 차이점을 C#과 비교하고 구조적 차이점을 확인하십시오.

VB.NET 위, condtion의

if(true){ 
//Do stuff 
} 

차이점 ()thenend if에 싸여

If True Then 
    'Do Stuff 
End If 

C 번호는 parenthesis로 대체된다. 코드를 곧바로 가져 와서 읽고 비교하면 안됩니다. 그런 다음 직접 시도하고 다시 작성하십시오.