Reality to Virtual in .NET


Imports System.Security.Principal

Public Class customprincipal : Implements Iprincipal
Shared users As Dictionary(Of String, user)

Shared Sub New()
users = New Dictionary(Of String, user)()
Dim user As user = user.Create(“Tom”, “tom”)
user.addrole(“Clerk”)
users.Add(user.name, user)
user = user.Create(“Jane”, “jane”)
user.addrole(“Clerk”)
user.addrole(“Manager”)
users.Add(user.name, user)

End Sub

Public Shared Function authenticateuser(ByVal username As String, ByVal password As String) As customprincipal
If users.ContainsKey(username) Then
Dim user As user = users(username)
If user.password.CompareTo(password) = 0 Then
Return New customprincipal(user.name, True, user.roles)
End If
End If

Return New customprincipal(username, False, Nothing)
End Function

Private roles As IList(Of String)
Private user As IIdentity

Public Sub New(ByVal userName As String, ByVal bAuthenticated As Boolean, ByVal roles As IList(Of String))
Me.user = New customidentity(userName, bAuthenticated)
Me.roles = roles

End Sub

Public ReadOnly Property Identity() As System.Security.Principal.IIdentity Implements System.Security.Principal.IPrincipal.Identity
Get
Return user
End Get
End Property

Public Function IsInRole(ByVal role As String) As Boolean Implements System.Security.Principal.IPrincipal.IsInRole
If roles Is Nothing Then Return False
Return roles.Contains(role)
End Function

Public Shared ReadOnly Property unautheticated() As customprincipal
Get
Return New customprincipal(“unaunthiticates”, False, Nothing)

End Get
End Property
End Class ‘customprincipal

Public Class user
Public name As String
Public password As String
Public roles As IList(Of String)

Public Shared Function create(ByVal name As String, ByVal password As String) As user
Dim user As New user()
user.roles = New List(Of String)()
user.name = name
user.password = password
Return user
End Function

Public Sub addrole(ByVal role As String)
roles.Add(role)
End Sub
End Class

Public Class customidentity : Implements IIdentity
Private username As String
Private bauthenticated As Boolean

Public Sub New(ByVal name As String, ByVal baunthenticates As Boolean)
Me.username = name
Me.bauthenticated = bauthenticated

End Sub
Public ReadOnly Property AuthenticationType() As String Implements System.Security.Principal.IIdentity.AuthenticationType
Get
Return “custom”
End Get
End Property

Public ReadOnly Property IsAuthenticated() As Boolean Implements System.Security.Principal.IIdentity.IsAuthenticated
Get
Return bauthenticated
End Get
End Property

Public ReadOnly Property Name() As String Implements System.Security.Principal.IIdentity.Name
Get
Return username
End Get
End Property
End Class

Advertisements

2 Responses

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: