locked
Unable to cast object of type 'System.Security.Principal.GenericIdentity' to type 'System.Web.Security.FormsIdentity'

    السؤال

  • I am getting the following error when i request the page in iis. it was working fine. if anybody give any solution for this is greatly appreciated

    Server Error in '/' Application.

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

    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.GenericIdentity' to type 'System.Web.Security.FormsIdentity'.

    Source Error:

    Line 15:         Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Line 16:             Response.Write("User Name : " + Server.HtmlEncode(User.Identity.Name))
    Line 17:             Dim id As FormsIdentity = CType(User.Identity, FormsIdentity)
    Line 18:             Response.Write("<br />Authentication Type :" & id.AuthenticationType.ToString)
    Line 19:             Dim ticket As FormsAuthenticationTicket = (id.Ticket)
    Source File: D:\ADFormQi\Default.aspx    Line: 17

    Stack Trace:

    [InvalidCastException: Unable to cast object of type 'System.Security.Principal.GenericIdentity' to type 'System.Web.Security.FormsIdentity'.]
       ASP.default_aspx.Page_Load(Object sender, EventArgs e) in D:\ADFormQi\Default.aspx:17
       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
       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.42
    27/ربيع الثاني/1427 04:30 ص

الإجابات

  • Hi,

    First off, I think you'll find more info in the ASP.NET > Security forum. See http://forums.asp.net/25/ShowForum.aspx

    It looks like your website expects you to use Forms Authentication. This means the user is presented with a login form. When you fill in the form and login, a Principal object that contains the right Identity is then attached to the current thread. You have to configure your application correctly in order to use Forms Authentication. The default way of authenticating (anonymous), will give you a Principal with a GenericIdentity object as the error states.

    See http://msdn2.microsoft.com/en-us/7t6b43z4.aspx for more information on Forms Authentication in ASP.NET 2.0.

    cheers,

    Stephane

    28/ربيع الثاني/1427 12:51 م
  • Thanks Stephane for your kind replay
    07/جمادى الأولى/1427 04:58 ص

جميع الردود

  • Hi,

    First off, I think you'll find more info in the ASP.NET > Security forum. See http://forums.asp.net/25/ShowForum.aspx

    It looks like your website expects you to use Forms Authentication. This means the user is presented with a login form. When you fill in the form and login, a Principal object that contains the right Identity is then attached to the current thread. You have to configure your application correctly in order to use Forms Authentication. The default way of authenticating (anonymous), will give you a Principal with a GenericIdentity object as the error states.

    See http://msdn2.microsoft.com/en-us/7t6b43z4.aspx for more information on Forms Authentication in ASP.NET 2.0.

    cheers,

    Stephane

    28/ربيع الثاني/1427 12:51 م
  • Thanks Stephane for your kind replay
    07/جمادى الأولى/1427 04:58 ص
  • I had this problem when creating my site http://top10central.com. This site includes a page where a login panel appears if the user is not already authenticated, but does not appear if the user is authenticated. I achieved this with a user control (with a .ascx extension), which creates a FormsAuthenticationTicket when the user registers.

     

    Under certain circumstances the page generates an email which requires the authenticated user's name, which would be stored in the userdata of the authentication ticket. If the user was not authenticated prior to opening this page - i.e. he used the login panel to register - then this 'Unable to cast...' error would occur when I tried to retrieve the user name from the authentication ticket, using the following code:

     

     dim id as FormsIdentity = CType(User.Identity, FormsIdentity)
       dim ticket as FormsAuthenticationTicket = id.Ticket
       sUserName = trim(ticket.UserData)

    I solved the problem with the following:

     

     If Page.User.Identity.IsAuthenticated then
            dim id as FormsIdentity = CType(User.Identity, FormsIdentity)
            dim ticket as FormsAuthenticationTicket = id.Ticket
            sUserName = trim(ticket.UserData)
       else
            sUserName = LoginPanel.UserName
      end if

     

    Where LoginPanel.UserName is a property of the LoginPanel user control that returns the user name as entered on the form. Hope that helps somebody!

    11/ربيع الأول/1428 09:39 ص
  • Hi all

    For those of you who haven't figure it out yet.

    I had the same problem. It appears that if the user isn't authenticated, you can't make the cast. If you try you will get the error:

    "Unable to cast object of type 'System.Security.Principal.GenericIdentity' to type 'System.Web.Security.FormsIdentity'. ".

    However, once authenticated, the cast is valid.  Hope that helps.


    • تم التحرير بواسطة mcohen2001 05/شعبان/1433 06:17 م
    • تم الاقتراح كإجابة بواسطة John SaundersModerator 06/رمضان/1433 02:12 ص
    05/شعبان/1433 06:16 م