Page view counter

Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'BusinessObjectLayer.CustomPrincipal'

Last post 04-13-2007 9:59 AM by scott@elbandit.co.uk. 2 replies.

Sort Posts:

  • Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'BusinessObjectLayer.CustomPrincipal'

    04-10-2007, 10:21 AM
     Any ideas why I get this message:

    Server Error in '/' Application.

    Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'BusinessObjectLayer.CustomPrincipal'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'BusinessObjectLayer.CustomPrincipal'.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [InvalidCastException: Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'BusinessObjectLayer.CustomPrincipal'.]
       Customer_Orders.Page_Load(Object sender, EventArgs e) +60
       System.Web.UI.Control.OnLoad(EventArgs e) +99
       System.Web.UI.Control.LoadRecursive() +47
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061
    



    Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210

    The error occurs here:

    Private
    aCustomer As Customer

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            aCustomer =
    CType(HttpContext.Current.User, CustomPrincipal).Customer
    End Sub

    BusinessObjectLayer.CustomPrincipal is:

    Imports System
    Imports System.Security.Principal

    '*********************************************************************
    ' CustomPrincipal Class
    '
    ' The CustomPrincipal class implements the IPrincipal interface so it
    ' can be used in place of the GenericPrincipal object. Requirements for
    ' implementing the IPrincipal interface include implementing the
    ' IIdentity interface and an implementation for IsInRole. The custom
    ' principal is attached to the current request in Global.asax in the
    ' Authenticate_Request event handler. The user's role is stored in the
    ' custom principal object in the Global_AcquireRequestState event handler.
    ''*********************************************************************

    Public

    Class CustomPrincipal
                    Implements IPrincipal

     

    Private _userRole As String = [String].Empty
    Private _aCustomer As Customer

    ' Required to implement the IPrincipal interface.
    Protected _Identity As IIdentity

    Public Sub New()

    End Sub 'New

    Public Sub New(ByVal identity As IIdentity, ByVal aCustomer As Customer)

               _Identity = identity

              _aCustomer = aCustomer

    End Sub 'New

    ' IIdentity property used to retrieve the Identity object attached to
    ' this principal.

    Public ReadOnly Property Identity() As IIdentity Implements IPrincipal.Identity
      Get

             Return _Identity
      End Get
    End Property

    Public Property UserRole() As String
      
    Get
          
    Return _userRole
      
    End Get
      
    Set(ByVal Value As String)
          _userRole = Value
       End Set
    End Property

    ' The Customer Object
    Public Property Customer() As Customer
       
    Get
            Return _aCustomer
       
    End Get
       
    Set(ByVal Value As Customer)
          _aCustomer = Value
       
    End Set
    End Property

    Public Function IsInRole(ByVal role As String) As Boolean Implements IPrincipal.IsInRole
         Return True
    End Function 'IsInRole

    End

    Class 'CustomPrincipal


    This is the code in Global.asax

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)

           ' Fires upon attempting to authenticate the user
           If Request.IsAuthenticated = True Then

                 Dim objCP As New CustomPrincipal

                 ' We need to get all information on the user and store it in our
                 ' Custom principal object

                 If HttpContext.Current.Cache(HttpContext.Current.User.Identity.Name) Is Nothing Then
                      
    ' Add our own custom principal to the request containing the user's identity, the user id, and~
                       ' the User's role

     

                          objCP =

    New CustomPrincipal(User.Identity, CustomerFunctions.getCustomerDetails(User.Identity.Name))

                          Context.User = objCP

                          HttpContext.Current.Cache.Insert(HttpContext.Current.User.Identity.Name, objCP,

    Nothing, Now.AddMinutes(10), Nothing)

                Else

                         objCP =

    CType(HttpContext.Current.Cache(HttpContext.Current.User.Identity.Name), CustomPrincipal)

                         Try
                                 Context.User = objCP
                         Catch ex As Exception

                         End Try

                 End If

          End If

    End Sub
    --------------------------------------------------------
    Don't forget to click "Mark as Answer" on the post(s) that helped you.

    Check out my book on learning all about enterprise programming. Professional Enterprise .NET

    NHibernate with ASP.net Problem-Design-Solution sample application
    Scott ASP.net blog

  • Re: Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'BusinessObjectLayer.CustomPrincipal'

    04-12-2007, 7:53 AM

    Hi

    Use your code to create a new website and it looks pretty well, I didn't receive any error.

    The only change I've made is setting the CustomerFunctions.getCustomerDetails(User.Identity.Name) to  New Customer , but it doesn't matter, right?

    BTW:  I use VS 2005 and .net v2.0.50727.

    Anyway, I guess it may because mismatched DLL versions. For detail information, please see this link and hope it helps

    http://p2p.wrox.com/TopicIndex/38283.htm

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards
    XiaoYong Dai
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'BusinessObjectLayer.CustomPrincipal'

    04-13-2007, 9:59 AM
    Answer
    hello XiaoYong Dai

    The problem appears to be that the Global.asax events were not firing. I have changed nothing in the Global.asax, I didn't touched the PrecompiledApp.config in the root of the site either. Do you know what could have caused this to stop working?
    --------------------------------------------------------
    Don't forget to click "Mark as Answer" on the post(s) that helped you.

    Check out my book on learning all about enterprise programming. Professional Enterprise .NET

    NHibernate with ASP.net Problem-Design-Solution sample application
    Scott ASP.net blog

Page 1 of 1 (3 items)