Click here to Skip to main content
12,552,516 members (27,182 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsPostBack Then
            If Request.IsAuthenticated Then
                Dim ident As FormsIdentity = CType(User.Identity, FormsIdentity)
                If ident IsNot Nothing Then
                    Dim ticket As FormsAuthenticationTicket = ident.Ticket
                    Dim userDataString As String = ticket.UserData
                    Dim userDataPieces() As String = userDataString.Split("|".ToCharArray())
                    Dim username As String = userDataPieces(1)
                    Dim rolename As String = userDataPieces(2)
                    If rolename <> "Graduate" Then
                        Response.Redirect("~/login.aspx", False)
                    End If
                End If
                Response.Redirect("~/login.aspx", False)
            End If
            lblerror.Visible = False
            Dim ident1 As FormsIdentity = CType(User.Identity, FormsIdentity)
            Dim ticket1 As FormsAuthenticationTicket = ident1.Ticket
            Dim userDataString1 As String = ticket1.UserData
            Dim userDataPieces1() As String = userDataString1.Split("|".ToCharArray())
            Dim userid As Integer = userDataPieces1(0)
            Dim ds As DataSet
            sqlquery("select offer.offerid as 'Offer ID', companyname + '(' + convert(varchar(50), company.companyid) + ')' as 'Company', request.requestid as 'Request ID', fname + ' ' + lname as 'Student', days as 'Days', minsalary as 'Minimum Salary', maxsalary as 'Maximum Salary' from company, offer, request, requestdetail, users where offer.offerid = requestdetail.offerid and offer.companyid = company.companyid and offer.userid = '" & userid & "' and request.userid = users.userid and request.requestid = requestdetail.requestid;")
            If ds.Tables(0).Rows.Count <> 0 Then
                gvrequestgrad.DataSource = ds.Tables(0)
                gvrequestgrad.SelectedIndex = -1
            End If 

this code is giving me an error while compiling the error is :

-Unable to cast object of type 'System.Security.Principal.WindowsIdentity' 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.WindowsIdentity' to type 'System.Web.Security.FormsIdentity'.

plz can anyone help me ?

[edit]Code block added, Urgency deleted - OriginalGriff[/edit]
Posted 3-Sep-13 6:10am
Updated 3-Sep-13 6:14am
Ron Beyer 3-Sep-13 12:12pm
"can anyone help me" Its just a code dump an an error, how about pointing out the line that you get the error on?
Member 10250266 3-Sep-13 18:59pm
it is all mentioned
OriginalGriff 3-Sep-13 12:14pm
Urgency deleted: It may be urgent to you, but it isn't to us. All that your stressing the urgency does is to make us think you have left it too late, and want us to do it for you. This annoys some people, and can slow a response.
Member 10250266 3-Sep-13 19:00pm
it is for a friend and i didn't know the solution so i know there is bright helpfull people in here so i drop the question
i m sry for annoying you but i m not forcing you to help me
It's not cast X to X, it's the cast X to Y.
Member 10250266 3-Sep-13 19:01pm
so do you know any solution ?
Solution for what? You cannot get FormsIdentity from WindowsIdentity. So, you should not do this cast. Just don't.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This line:
Dim ident As FormsIdentity = CType(User.Identity, FormsIdentity)

Change to:
Dim ident As IPrincipal = CType(User.Identity, IPrincipal)

FormsIdentity cannot be casted / converted from WindowsPrincipal
Member 10250266 3-Sep-13 17:56pm
i try it about ur solution but when i wrote that they give me a another error and underline under of word "ticket and they write that "ticket" isnt a member of 'System.Security.Principal.IPrincipal' so what i should change about definition of "ticket" ?!?!?! and tnx so much jlopez788 "Dim ticket As FormsAuthenticationTicket = ident.Ticket"
jlopez788 4-Sep-13 14:01pm
My suggestion is not to store it in the FormsAuthenticationTicket, instead store that information as a role.

IPrincipal has a function called IsInRole... or something along the lines,

"bool IsInRole(string role)"

the class inheriting from IPrincipal should be able to tell whether or not a user has a "Graduate" role. Thus eliminating the need for the cast
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

oh i will try it now thank you jlopez788
Ron Beyer 3-Sep-13 12:42pm
Please use the "Have a Question or Comment" box under the answer instead of posting a new answer as a comment, otherwise the poster of the answer won't get a notification of your comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.161021.1 | Last Updated 3 Sep 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100