Bagikan melalui


Cara: Membuat Objek GenericPrincipal dan GenericIdentity

Catatan

Artikel ini berlaku untuk Windows.

Untuk informasi tentang ASP.NET Core, lihat Gambaran umum Keamanan ASP.NET Core.

Anda dapat menggunakan GenericIdentity kelas bersama dengan GenericPrincipal kelas untuk membuat skema otorisasi yang tidak bergantung pada domain Windows.

Untuk membuat objek GenericPrincipal

  1. Membuat instans baru kelas identitas dan inisialisasikan dengan nama yang Anda inginkan untuk ditangguhkan. Kode berikut membuat objek GenericIdentity baru dan menginisialisasinya dengan nama MyUser.

    Dim myIdentity As New GenericIdentity("MyUser")
    
    GenericIdentity myIdentity = new GenericIdentity("MyUser");
    
  2. Buat instans baru kelas GenericPrincipal dan inisialisasikan dengan objek GenericIdentity yang dibuat sebelumnya dan array string yang mewakili peran yang ingin Anda kaitkan dengan prinsipal ini. Contoh kode berikut menentukan array string yang mewakili peran administrator dan peran pengguna. GenericPrincipal kemudian diinisialisasi dengan GenericIdentity sebelumnya dan array string.

    Dim myStringArray As String() = {"Manager", "Teller"}
    DIm myPrincipal As New GenericPrincipal(myIdentity, myStringArray)
    
    String[] myStringArray = {"Manager", "Teller"};
    GenericPrincipal myPrincipal = new GenericPrincipal(myIdentity, myStringArray);
    
  3. Menggunakan kode berikut untuk melampirkan prinsipal ke alur saat ini. Ini berharga dalam situasi di mana prinsipal harus divalidasi beberapa kali, itu harus divalidasi oleh kode lain yang berjalan dalam aplikasi Anda, atau harus divalidasi oleh PrincipalPermission objek. Anda masih dapat melakukan validasi berbasis peran pada objek utama tanpa melampirkannya ke alur. Untuk informasi selengkapnya, lihat Mengganti Objek Utama.

    Thread.CurrentPrincipal = myPrincipal
    
    Thread.CurrentPrincipal = myPrincipal;
    

Contoh

Contoh kode berikut menunjukkan cara membuat instans GenericPrincipal dan GenericIdentity. Kode ini menampilkan nilai objek ini ke konsol.

Imports System.Security.Principal
Imports System.Threading

Public Class Class1

    Public Shared Sub Main()
        ' Create generic identity.
        Dim myIdentity As New GenericIdentity("MyIdentity")

        ' Create generic principal.
        Dim myStringArray As String() =  {"Manager", "Teller"}
        Dim myPrincipal As New GenericPrincipal(myIdentity, myStringArray)

        ' Attach the principal to the current thread.
        ' This is not required unless repeated validation must occur,
        ' other code in your application must validate, or the
        ' PrincipalPermission object is used.
        Thread.CurrentPrincipal = myPrincipal

        ' Print values to the console.
        Dim name As String = myPrincipal.Identity.Name
        Dim auth As Boolean = myPrincipal.Identity.IsAuthenticated
        Dim isInRole As Boolean = myPrincipal.IsInRole("Manager")

        Console.WriteLine("The name is: {0}", name)
        Console.WriteLine("The isAuthenticated is: {0}", auth)
        Console.WriteLine("Is this a Manager? {0}", isInRole)

    End Sub

End Class
using System;
using System.Security.Principal;
using System.Threading;

public class Class1
{
    public static int Main(string[] args)
    {
    // Create generic identity.
    GenericIdentity myIdentity = new GenericIdentity("MyIdentity");

    // Create generic principal.
    String[] myStringArray = {"Manager", "Teller"};
    GenericPrincipal myPrincipal =
        new GenericPrincipal(myIdentity, myStringArray);

    // Attach the principal to the current thread.
    // This is not required unless repeated validation must occur,
    // other code in your application must validate, or the
    // PrincipalPermission object is used.
    Thread.CurrentPrincipal = myPrincipal;

    // Print values to the console.
    String name =  myPrincipal.Identity.Name;
    bool auth =  myPrincipal.Identity.IsAuthenticated;
    bool isInRole =  myPrincipal.IsInRole("Manager");

    Console.WriteLine("The name is: {0}", name);
    Console.WriteLine("The isAuthenticated is: {0}", auth);
    Console.WriteLine("Is this a Manager? {0}", isInRole);

    return 0;
    }
}

Saat dijalankan, aplikasi menampilkan output yang mirip dengan yang berikut ini.

The Name is: MyIdentity
The IsAuthenticated is: True
Is this a Manager? True

Lihat juga