ExtendingAttribute and Encryption

ExtendingAttribute and Encryption

Old forum URL: forums.lhotka.net/forums/t/9489.aspx


AKaplan posted on Thursday, September 09, 2010

So I thought of doing something kinda cool. You might be interested in this a little bit too... Back to dealing with my Library of objects, I have some properties that need to be encrypted and/or be used as an "extended attribute". Alot of my properties use the csla businessbase GetProperty method, and I thought of tweaking those up in order to handle the option to either be an extended attribute and/or encrypted. So within my library of objects, the property would look something like so:

  Public Property SSN() As String
        Get
            Return GetProperty(Of String)(_SSNProperty, _SSN)
        End Get
        Set(ByVal value As String)
            SetProperty(Of String)(_SSNProperty, _SSN, value)
        End Set
    End Property
turned into this... 
 Public Property SSN() As String
        Get
            Return GetProperty(Of String)(_SSNProperty, _SSN, False, True)
        End Get
        Set(ByVal value As String)
            SetProperty(Of String)(_SSNProperty, _SSN, value, False, True)
        End Set
    End Property
In the csla/Core/BusinessBase.vb file, I worked out the GetProperty function nicely, but the SetProperty method is giving me slightly more trouble...
Protected Function GetProperty(ByVal propertyInfo As IPropertyInfo, Optional ByVal extendedproperty As Boolean = False, Optional ByVal decrypt As Boolean = False) As Object
            Dim result As Object = Nothing
            If _bypassPropertyChecks OrElse CanReadProperty(propertyInfo.Name, False) Then
                Dim info = FieldManager.GetFieldData(propertyInfo)
                If info IsNot Nothing Then
                    result = info.Value
                End If
            Else
                result = propertyInfo.DefaultValue
            End If
            If extendedproperty = True Then
                result = ExtendedAttribute.GetExtendedAttribute(FieldManager.GetFieldData(propertyInfo).Name)
            End If
            If decrypt = True Then
                Dim data As Byte() = Convert.FromBase64String(CStr(result))
                result = AsymmetricEncryptionUtility.DecryptData(data, ConfigurationManager.AppSettings("RSA.AsymmetricalKey"))
            End If
            Return result
        End Function
The place where the newValue is located is giving me a bit of trouble considering that the attribute needs to take in a string value.
Protected Sub SetProperty(Of P)(ByVal propertyInfo As PropertyInfo(Of P), ByVal newValue As P, ByVal noAccess As Security.NoAccessBehavior, Optional ByVal extendedProperty As Boolean = False, Optional ByVal encrypt As Boolean = False)
            If _bypassPropertyChecks OrElse CanWriteProperty(propertyInfo.Name, noAccess = Security.NoAccessBehavior.ThrowException) Then
                Try
                    Dim oldValue As P = Nothing
                    Dim fieldData = FieldManager.GetFieldData(propertyInfo)
                    If fieldData Is Nothing Then
                        oldValue = propertyInfo.DefaultValue
                        fieldData = FieldManager.LoadFieldData(Of P)(propertyInfo, oldValue)
                    Else
                        Dim fd = TryCast(fieldData, FieldManager.IFieldData(Of P))
                        If fd IsNot Nothing Then
                            oldValue = fd.Value
                        Else
                            oldValue = DirectCast(fieldData.Value, P)
                        End If
                    End If
                    If GetType(P) Is GetType(String) AndAlso newValue Is Nothing Then
                        newValue = CoerceValue(Of P)(GetType(String), Nothing, String.Empty)
                    End If
                    If extendedProperty = True Then
                        ExtendedAttribute.SetExtendedAttribute(propertyInfo.Name, newValue)
                    End If
                    If encrypt = True Then
                        newValue = AsymmetricEncryptionUtility.EncryptData(newValue, ConfigurationManager.AppSettings("RSA.AsymmetricalKey"))
                    End If
                    LoadPropertyValue(Of P)(propertyInfo, oldValue, newValue, Not _bypassPropertyChecks)
                Catch ex As Exception
                    Throw New PropertyLoadException(String.Format(My.Resources.PropertyLoadException, propertyInfo.Name, ex.Message))
                End Try
            End If
        End Sub
Any ideas would be appreciated...

Copyright (c) Marimer LLC