質問

私は4GuysFromRollaのWebサイトからいくつかのコードを持っています。

私はそれのほとんどが働いていますが、コードのこの部分を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/:

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

他のヒント

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

私はあなたが残りの部分で大丈夫だと思います。

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 Anです 左側が左側にある場合にのみ、左側を評価する演算子 false (C#のデフォルトの動作)。

オブジェクトが何もないかどうかを確認するには、 null, そして、オブジェクトがタイプのものであるかどうかを確認するには、 is オペレーター。

どうですか

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は翻訳サービスではありませんが...

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

あなたが私たちにやるべきことは本質的に教えています C#.

私が最初に私のコメントに述べたように、わからない場合はコンバータを使用してください。しかしその後、あなたの違いを比較してください VB.NET あなたにコードをコードする C# そして構造的な違いを見てください。

vb.net

If True Then
  'Do Stuff
End If

C#

if(true){
 //Do stuff
}

上記の違いは、コンテンツが包まれています ()thenend if 置き換えられます parenthesis. 。あなたはまっすぐにコードを取り、それを読んでからそれを比較するべきではありません。それからあなた自身を再書き直す:)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top