错误:“无法将类型为'X'的对象强制转换为类型'X'" [英] Error :"Unable to cast object of type 'X' to type 'X' "
本文介绍了错误:“无法将类型为'X'的对象强制转换为类型'X'"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
受保护的 Sub Page_Load( ByVal 发件人作为 对象, ByVal e As System.EventArgs)
如果 不 IsPostBack 然后
如果 Request.IsAuthenticated 然后
Dim ident As FormsIdentity = < span class =code-keyword> CType (User.Identity,FormsIdentity)
如果 ident IsNot Nothing 然后
Dim ticket As FormsAuthenticationTicket = ident.Ticket
Dim userDataString As String = ticket.UserData
Dim userDataPieces() As String = userDataString.Split( | .ToCharArray())
Dim 用户名作为 字符串 = userDataPieces( 1 )
Dim rolename As String = userDataPieces( 2 )
如果 rolename<> 研究生 然后
Response.Redirect( 〜/ login.aspx,错误)
结束 如果
结束 如果
其他
Response.Redirect ( 〜/ login.aspx, False )
结束 如果
lblerror.Visible = False
Dim ident1 As FormsIdentity = CType (User.Identity,FormsIdentity)
Dim ticket1 As FormsAuthenticationTicket = ident1.Ticket
Dim userDataString1 As 字符串 = ticket1.UserData
Dim userDataPieces1()作为 字符串 = userDataString1.Split( | .ToCharArray ())
Dim userid As Integer = userDataPieces1( 0 )
Dim ds As DataSet
sqlquery( 选择offer.offerid作为'Offer ID',companyname +'('+ convert(varchar(50),company.companyid)+')'作为'Company',request.requestid作为'Request ID', fname +''+ lname为'Student',days为'Days',minsalary为'Minimum Salary',maxsalary为'Maximum Salary',来自公司,offer,request,requestdetail,users for offer.offerid = requestdetail.offerid and offer .companyid = company.companyid和offer.userid ='&用户ID和 '和request.userid = users.userid and request.requestid = requestdetail.requestid;)
如果 ds.Tables( 0 )。Rows.Count<> 0 然后
gvrequestgrad.DataSource = ds.Tables( 0 )
gvrequestgrad.DataBind()
gvrequestgrad.SelectedIndex = -1
结束 如果
此代码在编译错误时给出错误:
- 无法将System.Security.Principal.WindowsIdentity类型的对象强制转换为System.Web.Security.FormsIdentity。
- 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
-Exception Details zh_cn:System.InvalidCastException:无法将类型为System.Security.Principal.WindowsIdentity的对象强制转换为'System.Web.Security.FormsIdentity'。
PLZ任何人都可以帮助我吗?
代码块添加,紧急删除 - OriginalGriff [/ edit]
解决方案
这一行:
Dim ident As FormsIdentity = CType (User.Identity, FormsIdentity)
更改为:
Dim ident As IPrincipal = CType (User.Identity,IPrincipal)
FormsIdentity无法从WindowsPrincipal进行转换/转换
哦,我现在试试吧谢谢jlopez788
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
Else
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.DataBind()
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]
解决方案
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
oh i will try it now thank you jlopez788
这篇关于错误:“无法将类型为'X'的对象强制转换为类型'X'"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文