Unable to cast object of type 'System.Web.Security.FormsIdentity' to type 'XXX'

I got following

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            if (Request.IsAuthenticated)
            {

                var user = Membership.GetUser();

                var identity = new CustomIdentity(user.UserName, new Guid(user.ProviderUserKey.ToString()));

                var principal = new RolePrincipal(identity);
                System.Threading.Thread.CurrentPrincipal = principal;
            }

        }

 

But when i access  System.Threading.Thread.CurrentPrincipal and cast it to my CustomIdentity class,which inherit from GenericIdentity

    public class CustomIdentity : GenericIdentity
    {
        public CustomIdentity(string name, Guid userId)
            : base(name)
        {
            this.UserId = userId;
        }

        public Guid UserId { get; set; }
    }

But when in business layer i try to access  System.Threading.Thread.CurrentPrincipal

((CustomIdentity) System.Threading.Thread.CurrentPrincipal.Identity).UserId; 

and cast it to CustomIdentity,im getting error:

Unable to cast object of type 'System.Web.Security.FormsIdentity' to type 'CustomIdentity'.

 Thank you for your help
 

 

0
Adult
8/29/2008 3:43:07 AM
asp.net.security 27051 articles. 1 followers. Follow

3 Replies
1088 Views

Similar Articles

[PageSpeed] 54

 any ideas?

0
Adult
8/29/2008 4:42:10 PM
also having this problem ? anyone have a solution ?
0
mike123
9/27/2008 11:09:38 AM

 Hi,

 

I just moved my app to iis7 and i am now getting this error. Everything worked fine under iis6.

 

Now my security is completely broken. i'll include a snippet of my code and my error. Any help would be greatly appreciated. Thank you.

 

error: Unable to cast object of type 'System.Web.Security.FormsIdentity' to type 'cbgIdentity'

 

Imports System.Security.Principal
Imports System.IO
<Serializable()> _
1    Public Class cbgIdentity
2        Implements IIdentity
3    
4        Dim _name As String = String.Empty
5        Dim _isAuthenticated As Boolean = False
6        Dim _id As Integer = -1
7        Dim _roles As IList = New ArrayList
8    
9        Public Sub New(ByVal name As String, ByVal id As Int32, ByVal isAuthenticated As Boolean, ByVal roles As IList)
10           _name = name
11           _id = id
12           _isAuthenticated = isAuthenticated
13           If Not Nothing Is roles Then
14               _roles = roles
15           End If
16       End Sub
17       Public Shared Function Serialize(ByRef identity As cbgIdentity) As String
18           Dim lf As LosFormatter = New LosFormatter
19           Dim sw As StringWriter = New StringWriter
20           lf.Serialize(sw, identity)
21           Return sw.ToString()
22       End Function
23   
24       Public Shared Function Deserialize(ByRef identityString As String) As cbgIdentity
25           Try
26               Dim lf As LosFormatter = New LosFormatter
27               Return CType(lf.Deserialize(identityString), cbgIdentity)
28           Catch e As System.Exception
29               ' TODO: write log entry 
30               Return Nothing
31           End Try
32       End Function
33   
34   #Region "Properties"
35       Public Overridable ReadOnly Property Name() As String Implements IIdentity.Name
36           Get
37               Return _name
38           End Get
39       End Property
40       Public Overridable ReadOnly Property AuthenticationType() As String Implements IIdentity.AuthenticationType
41           Get
42               Return "mysecure"
43           End Get
44       End Property
45       Public Overridable ReadOnly Property IsAuthenticated() As Boolean Implements IIdentity.IsAuthenticated
46           Get
47               Return _isAuthenticated
48           End Get
49       End Property
50       Public ReadOnly Property ID() As Int32
51           Get
52               Return _id
53           End Get
54       End Property
55       Public Property Roles() As IList
56           Get
57               Return _roles
58           End Get
59           Set(ByVal Value As IList)
60   
61           End Set
62       End Property
63   #End Region
64   End Class
 

 
0
robertc
9/27/2008 11:23:20 AM
Reply: