how to add another property to class inheriting from IIdentity?

Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default how to add another property to class inheriting from IIdentity?

    Hello peoples
    this is a question regarding security of windows application
    I want to add another property (the UserID) to System.Security.Principal.IIdentity
    My code looks logical (at least to me) and does show the property 'UserID' in the Intellisense
    But when I try to use it anywhere throughout the code doesn't let me do the following
    Thread.CurrentPrincipal.Identity.UserI
    It only comes up with the default IIdentity properties AuthenticationType, IsAuthenticated and Name

    Did I do something wrong, or is there really no way to have an additional property (like my UserID)

    Any hints are much appreciated

    Here's the code

    Imports Syste
    Imports System.Threadin
    Imports System.Security.Principa

    Public Class SecurityBU
    Public Class SecIdentit
    Implements IIdentit

    Public ConnectionString As Strin
    Private _UserID As Integer =
    Private _Username As String = "
    Private _Roles As New ArrayLis

    #Region " IIdentity

    Public ReadOnly Property AuthenticationType() As String Implements System.Security.Principal.IIdentity.Authentication Typ
    Ge
    Return "Special
    End Ge
    End Propert

    Public ReadOnly Property IsAuthenticated() As Boolean Implements System.Security.Principal.IIdentity.IsAuthenticate
    Ge
    Return Len(Me._Username) >
    End Ge
    End Propert

    Public ReadOnly Property Name() As String Implements System.Security.Principal.IIdentity.Nam
    Ge
    Return Me._Usernam
    End Ge
    End Propert
    #End Regio

    Public ReadOnly Property UserID() As Intege
    Ge
    Return Me._UserI
    End Ge
    End Propert

    Friend Function IsInRole(ByVal strRole As String) As Boolea
    Return Me._Roles.Contains(strRole
    End Functio

    Friend Function LoadIdentity(ByVal strUser As String, ByVal strPw As String) As
    BusinessRules.SecurityBUS.SecIdentit

    Dim myDAL As New DataAccess.SecurityDA
    Dim myTable As New DataTabl
    Dim myDR As DataRo
    Tr
    myDAL.CS_ConnectString = Me.ConnectionStrin
    myTable = myDAL.LoadIdentity(strUser, strPw

    Me._UserID =
    Me._Username = "
    Me._Roles.Clear(

    If Not myTable Is Nothing The
    If myTable.Rows.Count > 0 The
    '--user is authenticate
    Me._UserID = myTable.Rows(0).Item("UserID"
    Me._Username = myTable.Rows(0).Item("Username"

    For Each myDR In myTable.Row
    Me._Roles.Add(CStr(myDR.Item("Role"))
    Nex

    End I
    End I

    Catch ex As Exceptio
    Throw New System.Exception("Error: In module SecurityBUS.GetData --> " + vbCrLf + ex.Message
    End Tr

    Return M

    End Functio

    End Clas

    Public Class SecPrincipa
    Implements IPrincipa

    Public ConnectionString As Strin
    Private _Identity As SecIdentit

    #Region " IPrincipal

    Public ReadOnly Property Identity() As System.Security.Principal.IIdentity Implements System.Security.Principal.IPrincipal.Identit
    Ge
    Return Me._Identit
    End Ge
    End Propert

    Public Function IsInRole(ByVal role As String) As Boolean Implements System.Security.Principal.IPrincipal.IsInRol
    Return Me._Identity.IsInRole(role
    End Functio

    #End Regio

    #Region " Login Process

    Public Sub Login(ByVal strUsername As String, ByVal strPassword As String

    Dim currentdomain As AppDomain = Thread.GetDomai
    currentdomain.SetPrincipalPolicy(PrincipalPolicy.U nauthenticatedPrincipal

    Dim OldPrincipal As IPrincipal = Thread.CurrentPrincipa
    Thread.CurrentPrincipal = Me

    Try
    If Not TypeOf OldPrincipal Is BusinessRules.SecurityBUS.SecPrincipal Then
    currentdomain.SetThreadPrincipal(Me)

    End If
    Catch ex As Exception
    '--Failed, but we don't care because there's nothing we can do in this case
    End Try

    '--Load the underlying identity object that tells whether we are really logged in.
    '--If so, will contain the list of roles we belong to.
    Dim myIdentity As New BusinessRules.SecurityBUS.SecIdentity
    myIdentity.ConnectionString = Me.ConnectionString
    Me._Identity = myIdentity.LoadIdentity(strUsername, strPassword)

    End Sub

    #End Region
    End Class

    End Class


    ...thanks for having a look. Klaus.

    ---
    Posted using Wimdows.net Newsgroups - [url]http://www.wimdows.net/newsgroups/[/url]
    Klaus Guest

  2. Similar Questions and Discussions

    1. Allowing a new design property for control inheriting DataGrid
      Hi, I created a custom control inheriting from DataGrid I would like to put a custom property for design time manipulation I added the...
    2. Help Anyone?How do I access Inner Class Property
      hi guys, I am having this problems too... if I inherits it from System.Web.UI.WebControl.WebControls.. it works fine.. but if I inherits it from...
    3. Binding a textbox to a class property
      This might sound like a elementary question but I've haven't been able to figure this one out. How can I bind the "text" property of a "textBox"...
    4. undefined class property
      I've got this $user class that has a variable called $userIsSuperRoot. When I just set error_reporting to ALL I got an error on the last line that...
    5. Class property misunderstanding
      Hi Jim, What you're dealing with here is scope. Variables have scope, which means that they are accessible at different levels depending upon how...
  3. #2

    Default Re: how to add another property to class inheriting from IIdentity?

    Actually you can't modify IIdentity and add new members. However, you might
    derive your own interface from IIdentity and and your custom user profile
    data or whatever info you like. If you want a further approach on how to
    design a more robust approach on this topic you can check it out the
    Authorization & Profiling Application Block
    ([url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html[/url]
    /authpro.asp).
    That strategy is very extensible but not as simple as the IIdentity
    solution.
    I hope this help you.

    --
    Hernan de Lahitte
    Lagash Systems S.A.
    [url]http://weblogs.asp.net/hernandl[/url]


    This posting is provided "AS IS" with no warranties, and confers no rights.

    "Klaus" <klaus@-NOSPAM-kitchen-net.com.au> wrote in message
    news:eyIKGV6TEHA.2940@TK2MSFTNGP09.phx.gbl...
    > Hello peoples,
    > this is a question regarding security of windows application.
    > I want to add another property (the UserID) to
    System.Security.Principal.IIdentity.
    > My code looks logical (at least to me) and does show the property 'UserID'
    in the Intellisense.
    > But when I try to use it anywhere throughout the code doesn't let me do
    the following:
    > Thread.CurrentPrincipal.Identity.UserID
    > It only comes up with the default IIdentity properties AuthenticationType,
    IsAuthenticated and Name.
    >
    > Did I do something wrong, or is there really no way to have an additional
    property (like my UserID)?
    >
    > Any hints are much appreciated.
    >
    > Here's the code.
    >
    > Imports System
    > Imports System.Threading
    > Imports System.Security.Principal
    >
    > Public Class SecurityBUS
    > Public Class SecIdentity
    > Implements IIdentity
    >
    > Public ConnectionString As String
    > Private _UserID As Integer = 0
    > Private _Username As String = ""
    > Private _Roles As New ArrayList
    >
    > #Region " IIdentity "
    >
    > Public ReadOnly Property AuthenticationType() As String Implements
    System.Security.Principal.IIdentity.Authentication Type
    > Get
    > Return "Special"
    > End Get
    > End Property
    >
    > Public ReadOnly Property IsAuthenticated() As Boolean Implements
    System.Security.Principal.IIdentity.IsAuthenticate d
    > Get
    > Return Len(Me._Username) > 0
    > End Get
    > End Property
    >
    > Public ReadOnly Property Name() As String Implements
    System.Security.Principal.IIdentity.Name
    > Get
    > Return Me._Username
    > End Get
    > End Property
    > #End Region
    >
    > Public ReadOnly Property UserID() As Integer
    > Get
    > Return Me._UserID
    > End Get
    > End Property
    >
    > Friend Function IsInRole(ByVal strRole As String) As Boolean
    > Return Me._Roles.Contains(strRole)
    > End Function
    >
    > Friend Function LoadIdentity(ByVal strUser As String, ByVal strPw
    As String) As _
    >
    BusinessRules.SecurityBUS.SecIdentity
    >
    > Dim myDAL As New DataAccess.SecurityDAL
    > Dim myTable As New DataTable
    > Dim myDR As DataRow
    > Try
    > myDAL.CS_ConnectString = Me.ConnectionString
    > myTable = myDAL.LoadIdentity(strUser, strPw)
    >
    > Me._UserID = 0
    > Me._Username = ""
    > Me._Roles.Clear()
    >
    > If Not myTable Is Nothing Then
    > If myTable.Rows.Count > 0 Then
    > '--user is authenticated
    > Me._UserID = myTable.Rows(0).Item("UserID")
    > Me._Username = myTable.Rows(0).Item("Username")
    >
    > For Each myDR In myTable.Rows
    > Me._Roles.Add(CStr(myDR.Item("Role")))
    > Next
    >
    > End If
    > End If
    >
    > Catch ex As Exception
    > Throw New System.Exception("Error: In module
    SecurityBUS.GetData --> " + vbCrLf + ex.Message)
    > End Try
    >
    > Return Me
    >
    > End Function
    >
    > End Class
    >
    > Public Class SecPrincipal
    > Implements IPrincipal
    >
    > Public ConnectionString As String
    > Private _Identity As SecIdentity
    >
    > #Region " IPrincipal "
    >
    > Public ReadOnly Property Identity() As
    System.Security.Principal.IIdentity Implements
    System.Security.Principal.IPrincipal.Identity
    > Get
    > Return Me._Identity
    > End Get
    > End Property
    >
    > Public Function IsInRole(ByVal role As String) As Boolean
    Implements System.Security.Principal.IPrincipal.IsInRole
    > Return Me._Identity.IsInRole(role)
    > End Function
    >
    > #End Region
    >
    > #Region " Login Process "
    >
    > Public Sub Login(ByVal strUsername As String, ByVal strPassword As
    String)
    >
    > Dim currentdomain As AppDomain = Thread.GetDomain
    >
    currentdomain.SetPrincipalPolicy(PrincipalPolicy.U nauthenticatedPrincipal)
    >
    > Dim OldPrincipal As IPrincipal = Thread.CurrentPrincipal
    > Thread.CurrentPrincipal = Me
    >
    > Try
    > If Not TypeOf OldPrincipal Is
    BusinessRules.SecurityBUS.SecPrincipal Then
    > currentdomain.SetThreadPrincipal(Me)
    >
    > End If
    > Catch ex As Exception
    > '--Failed, but we don't care because there's nothing we
    can do in this case
    > End Try
    >
    > '--Load the underlying identity object that tells whether we
    are really logged in.
    > '--If so, will contain the list of roles we belong to.
    > Dim myIdentity As New BusinessRules.SecurityBUS.SecIdentity
    > myIdentity.ConnectionString = Me.ConnectionString
    > Me._Identity = myIdentity.LoadIdentity(strUsername,
    strPassword)
    >
    > End Sub
    >
    > #End Region
    > End Class
    >
    > End Class
    >
    >
    > ..thanks for having a look. Klaus.
    >
    > ---
    > Posted using Wimdows.net Newsgroups - [url]http://www.wimdows.net/newsgroups/[/url]

    Hernan de Lahitte Guest

  4. #3

    Default Re: how to add another property to class inheriting from IIdentity?

    To be honest, I didn't have a real good look through the code you provided,
    but... you should be able to cast the identity object to your custom
    identity object to get access to the properties you added (provided you
    previously assigned an identity object of your custom identity type). I am a
    C# person, but I beleive you need to "CType" the thread identity to your
    custom identity type.

    --
    - Paul Glavich
    Microsoft MVP - ASP.NET


    "Hernan de Lahitte" <hernan@lagash.com> wrote in message
    news:ufxogd7TEHA.1036@TK2MSFTNGP09.phx.gbl...
    > Actually you can't modify IIdentity and add new members. However, you
    might
    > derive your own interface from IIdentity and and your custom user profile
    > data or whatever info you like. If you want a further approach on how to
    > design a more robust approach on this topic you can check it out the
    > Authorization & Profiling Application Block
    >
    ([url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html[/url]
    > /authpro.asp).
    > That strategy is very extensible but not as simple as the IIdentity
    > solution.
    > I hope this help you.
    >
    > --
    > Hernan de Lahitte
    > Lagash Systems S.A.
    > [url]http://weblogs.asp.net/hernandl[/url]
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    rights.
    >
    > "Klaus" <klaus@-NOSPAM-kitchen-net.com.au> wrote in message
    > news:eyIKGV6TEHA.2940@TK2MSFTNGP09.phx.gbl...
    > > Hello peoples,
    > > this is a question regarding security of windows application.
    > > I want to add another property (the UserID) to
    > System.Security.Principal.IIdentity.
    > > My code looks logical (at least to me) and does show the property
    'UserID'
    > in the Intellisense.
    > > But when I try to use it anywhere throughout the code doesn't let me do
    > the following:
    > > Thread.CurrentPrincipal.Identity.UserID
    > > It only comes up with the default IIdentity properties
    AuthenticationType,
    > IsAuthenticated and Name.
    > >
    > > Did I do something wrong, or is there really no way to have an
    additional
    > property (like my UserID)?
    > >
    > > Any hints are much appreciated.
    > >
    > > Here's the code.
    > >
    > > Imports System
    > > Imports System.Threading
    > > Imports System.Security.Principal
    > >
    > > Public Class SecurityBUS
    > > Public Class SecIdentity
    > > Implements IIdentity
    > >
    > > Public ConnectionString As String
    > > Private _UserID As Integer = 0
    > > Private _Username As String = ""
    > > Private _Roles As New ArrayList
    > >
    > > #Region " IIdentity "
    > >
    > > Public ReadOnly Property AuthenticationType() As String
    Implements
    > System.Security.Principal.IIdentity.Authentication Type
    > > Get
    > > Return "Special"
    > > End Get
    > > End Property
    > >
    > > Public ReadOnly Property IsAuthenticated() As Boolean Implements
    > System.Security.Principal.IIdentity.IsAuthenticate d
    > > Get
    > > Return Len(Me._Username) > 0
    > > End Get
    > > End Property
    > >
    > > Public ReadOnly Property Name() As String Implements
    > System.Security.Principal.IIdentity.Name
    > > Get
    > > Return Me._Username
    > > End Get
    > > End Property
    > > #End Region
    > >
    > > Public ReadOnly Property UserID() As Integer
    > > Get
    > > Return Me._UserID
    > > End Get
    > > End Property
    > >
    > > Friend Function IsInRole(ByVal strRole As String) As Boolean
    > > Return Me._Roles.Contains(strRole)
    > > End Function
    > >
    > > Friend Function LoadIdentity(ByVal strUser As String, ByVal
    strPw
    > As String) As _
    > >
    > BusinessRules.SecurityBUS.SecIdentity
    > >
    > > Dim myDAL As New DataAccess.SecurityDAL
    > > Dim myTable As New DataTable
    > > Dim myDR As DataRow
    > > Try
    > > myDAL.CS_ConnectString = Me.ConnectionString
    > > myTable = myDAL.LoadIdentity(strUser, strPw)
    > >
    > > Me._UserID = 0
    > > Me._Username = ""
    > > Me._Roles.Clear()
    > >
    > > If Not myTable Is Nothing Then
    > > If myTable.Rows.Count > 0 Then
    > > '--user is authenticated
    > > Me._UserID = myTable.Rows(0).Item("UserID")
    > > Me._Username = myTable.Rows(0).Item("Username")
    > >
    > > For Each myDR In myTable.Rows
    > > Me._Roles.Add(CStr(myDR.Item("Role")))
    > > Next
    > >
    > > End If
    > > End If
    > >
    > > Catch ex As Exception
    > > Throw New System.Exception("Error: In module
    > SecurityBUS.GetData --> " + vbCrLf + ex.Message)
    > > End Try
    > >
    > > Return Me
    > >
    > > End Function
    > >
    > > End Class
    > >
    > > Public Class SecPrincipal
    > > Implements IPrincipal
    > >
    > > Public ConnectionString As String
    > > Private _Identity As SecIdentity
    > >
    > > #Region " IPrincipal "
    > >
    > > Public ReadOnly Property Identity() As
    > System.Security.Principal.IIdentity Implements
    > System.Security.Principal.IPrincipal.Identity
    > > Get
    > > Return Me._Identity
    > > End Get
    > > End Property
    > >
    > > Public Function IsInRole(ByVal role As String) As Boolean
    > Implements System.Security.Principal.IPrincipal.IsInRole
    > > Return Me._Identity.IsInRole(role)
    > > End Function
    > >
    > > #End Region
    > >
    > > #Region " Login Process "
    > >
    > > Public Sub Login(ByVal strUsername As String, ByVal strPassword
    As
    > String)
    > >
    > > Dim currentdomain As AppDomain = Thread.GetDomain
    > >
    > currentdomain.SetPrincipalPolicy(PrincipalPolicy.U nauthenticatedPrincipal)
    > >
    > > Dim OldPrincipal As IPrincipal = Thread.CurrentPrincipal
    > > Thread.CurrentPrincipal = Me
    > >
    > > Try
    > > If Not TypeOf OldPrincipal Is
    > BusinessRules.SecurityBUS.SecPrincipal Then
    > > currentdomain.SetThreadPrincipal(Me)
    > >
    > > End If
    > > Catch ex As Exception
    > > '--Failed, but we don't care because there's nothing we
    > can do in this case
    > > End Try
    > >
    > > '--Load the underlying identity object that tells whether we
    > are really logged in.
    > > '--If so, will contain the list of roles we belong to.
    > > Dim myIdentity As New BusinessRules.SecurityBUS.SecIdentity
    > > myIdentity.ConnectionString = Me.ConnectionString
    > > Me._Identity = myIdentity.LoadIdentity(strUsername,
    > strPassword)
    > >
    > > End Sub
    > >
    > > #End Region
    > > End Class
    > >
    > > End Class
    > >
    > >
    > > ..thanks for having a look. Klaus.
    > >
    > > ---
    > > Posted using Wimdows.net Newsgroups - [url]http://www.wimdows.net/newsgroups/[/url]
    >
    >

    Paul Glavich [MVP - ASP.NET] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139