ASP.NET 2.0 + Error + Roles

Posted on 2009-04-25
Last Modified: 2013-11-07
I am trying to implement a basic Roles class but get the following error, but have no idea what it means, can someone please help


Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'SitePrincipal'.
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 'SitePrincipal'.

Source Error:

Line 17:         {
Line 18:
Line 19:             if (!((SitePrincipal)Context.User).IsInRole("Site Admin"))
Line 20:                 Response.Redirect("../Login.aspx");
Line 21:
Question by:dkilby
    1 Comment
    LVL 3

    Accepted Solution

    If you are using Roles and Forms authentication, you can set the permission in the web.config file. It will automatically direct users to the login page if they are not members of the specified role.

    However, if you want to do it this way, off the top of my head, try just typing it as


    or use

    HttpContext.Current.User.IsInRole("Site Admin")

    is SitePrincipal your own implementation?

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Hello, In my precious Article  ( saw the Configuration part for Microstrategy which included Metadata Creation and DataSource Preparation as …
    This video discusses moving either the default database or any database to a new volume.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now