0

IDがパラメータとともに渡されたユーザーが管理者であるかどうかを判別する関数を作成したいと思います。私は現在ログインしているユーザーに対してこれを行うことができます-

        public static bool IsAuthorizedUser()
    {

        WindowsIdentity identity = WindowsIdentity.GetCurrent(); 
        WindowsPrincipal principal = new WindowsPrincipal(identity); 
        return principal.IsInRole(WindowsBuiltInRole.Administrator); 
    }

ただし、渡されたユーザーを確認したいので、署名は次のように変更されます。

public static bool IsAuthorizedUser(string username_to_check) 

これどうやってするの?どんな助けでも大歓迎です。

4

3 に答える 3

1

UserPrincipalで思い通りに動作させることができました。すべてのフィードバックに感謝します。

 public static bool IsAuthorizedUser(string userId) 
 {
     PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
     UserPrincipal usr = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userId);

     bool result = false;
     if(usr == null)
     {
         Console.WriteLine("usr is null");
     }
     else
     {
         foreach (Principal p in usr.GetAuthorizationGroups())
         {
             if (p.ToString() == "Administrators")
             {
                 result = true;
             }
         }
      }
      return result;
    }
于 2012-07-02T15:52:25.400 に答える
0

WindowsIdentityの宣言を調べて、その文字列名からユーザーIDを取得するメソッドがあるかどうかを確認する必要があるようです。

于 2012-06-28T19:19:29.283 に答える
0

このコードで試すことができます

if(principal.Identity.IsAuthenticated)
{
    //Passed
}
于 2012-06-28T19:20:56.663 に答える