英語で読む

次の方法で共有


Thread.CurrentPrincipal プロパティ

定義

ロールベースのセキュリティに関する、スレッドの現在のプリンシパルを取得または設定します。

public static System.Security.Principal.IPrincipal? CurrentPrincipal { get; set; }
public static System.Security.Principal.IPrincipal CurrentPrincipal { get; set; }

プロパティ値

セキュリティ コンテキストを表す IPrincipal 値。

例外

呼び出し元に、プリンシパルを設定するために必要なアクセス許可がありません。

次のコード例は、スレッドのプリンシパルを設定および取得する方法を示しています。

using System;
using System.Security;
using System.Security.Permissions;
using System.Security.Principal;
using System.Threading;

class Principal
{
    static void Main()
    {
        string[] rolesArray = {"managers", "executives"};
        try
        {
            // Set the principal to a new generic principal.
            Thread.CurrentPrincipal = 
                new GenericPrincipal(new GenericIdentity(
                "Bob", "Passport"), rolesArray);
        }
        catch(SecurityException secureException)
        {
            Console.WriteLine("{0}: Permission to set Principal " +
                "is denied.", secureException.GetType().Name);
        }

        IPrincipal threadPrincipal = Thread.CurrentPrincipal;
        Console.WriteLine("Name: {0}\nIsAuthenticated: {1}" +
            "\nAuthenticationType: {2}", 
            threadPrincipal.Identity.Name, 
            threadPrincipal.Identity.IsAuthenticated,
            threadPrincipal.Identity.AuthenticationType);
    }
}

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1