Custom Security

Custom Security

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


brannos posted on Friday, February 06, 2009

I know we all have seen questions like this one come up on several occasions and I have read them but I am still having some problems.

I have build my custom security but I can't access it so I can utilize it in my application. I think it has something to do with the need to directcast it. So in the SetPrincipal in my custom principal I have the following:

   If identity.IsAuthenticated Then

      Dim principal As CMIPrincipal = DirectCast(Csla.ApplicationContext.User, CMIPrincipal)

      principal = New CMIPrincipal(identity)

      Csla.ApplicationContext.User = Principal

   End If

   Return identity.IsAuthenticated

I know this is not correct but it was my last ditch effort to have intellisence and the framework see my code and not .net or csla security code. Any Ideas how to accomplish this, could not find an answer.

RockfordLhotka replied on Saturday, February 07, 2009

If you are using a custom principal and/or identity type that has extra properties or methods you can't escape the need to cast the .NET IPrincipal/IIdentity objects to your specific type.

What most people do is create a Module or static class that does the work:

Public Module Security
  Public ReadOnly Property Name() As String
    Get
      Return DirectCast(Csla.ApplicationContext.User, CMIPrincipal).Name
    End Get
  End Property
End Module

Just add all the properties/methods from your principal and identity to this Security type, then throughout your code you just use Security:

If Security.IsInRole("blah") Then

Console.WriteLine(Security.Name)

MessageBox.Show("User " & Security.Name & " is in " & Security.Department)

It doesn't change the need to cast the value - but it abstracts the work into a central location.

Copyright (c) Marimer LLC