forms identity problem

D

Darin Shaw

I'm receiving a "specified cast is invalid" with the following code, though
I've used the exact same code in other applications with success. Can anyone
tell me what I'm missing or if there are other factors to be considered
here?
Thanks in advance.

========
Private Sub Page_Load(ByVal sender as System.Object, ByVal e as
System.EventArgs) Handles MyBase.Load

Dim objFormsID as FormsIdentity
objFormsID = User.Identity <=== specified cast
invalid
....
End Sub
========
 
J

John Saunders

Darin Shaw said:
I'm receiving a "specified cast is invalid" with the following code, though
I've used the exact same code in other applications with success. Can anyone
tell me what I'm missing or if there are other factors to be considered
here?
Thanks in advance.

========
Private Sub Page_Load(ByVal sender as System.Object, ByVal e as
System.EventArgs) Handles MyBase.Load

Dim objFormsID as FormsIdentity
objFormsID = User.Identity <=== specified cast
invalid

So, I guess User.Identity is not a FormsIdentity.
 
D

Darin Shaw

As that reply was quite unhelpful, perhaps I have not been clear enough.
I'm curious as to why this code is used successfully in text books and has
worked in other web applications of mine, but does not work in this one. Can
anyone offer any insight as to what criteria must be met for this cast to be
made, or why this would incur the stated runtime error in one case but not
another?
Thanks in advance.
 
J

John Saunders

Because in your instance, User.Identity does not reference a FormsIdentity,
but rather some other type of object. I recommend you use the debugger to
find out what kind of object it is. If you'd rather not, try
Response.Write(User.Identity.GetType().FullName), and put a REM in front of
the line which fails.

If this question is still not helpful, perhaps you could tell us what part
of it you don't understand.

--
John

Darin Shaw said:
As that reply was quite unhelpful, perhaps I have not been clear enough.
I'm curious as to why this code is used successfully in text books and has
worked in other web applications of mine, but does not work in this one. Can
anyone offer any insight as to what criteria must be met for this cast to be
made, or why this would incur the stated runtime error in one case but not
another?
Thanks in advance.
 
D

Darin Shaw

I know what type it is. It's an IIdentity.
My question was, and is, why it is able to cast an IIdentity to the
FormsIdentity variable in some cases, but not others.

John Saunders said:
Because in your instance, User.Identity does not reference a FormsIdentity,
but rather some other type of object. I recommend you use the debugger to
find out what kind of object it is. If you'd rather not, try
Response.Write(User.Identity.GetType().FullName), and put a REM in front of
the line which fails.

If this question is still not helpful, perhaps you could tell us what part
of it you don't understand.

--
John

Darin Shaw said:
As that reply was quite unhelpful, perhaps I have not been clear enough.
I'm curious as to why this code is used successfully in text books and has
worked in other web applications of mine, but does not work in this one. Can
anyone offer any insight as to what criteria must be met for this cast
to
 
J

John Saunders

Darin Shaw said:
I know what type it is. It's an IIdentity.
My question was, and is, why it is able to cast an IIdentity to the
FormsIdentity variable in some cases, but not others.

No, you don't know.

IIdentity is an interface. Many different classes implement IIdentity.
FormsIdentity is only one of them. GenericIdentity and WindowsIdentity are
others. Neither of the latter two are the same as FormsIdentity, so neither
can be cast to FormsIdentity.

For some reason, you're getting a different type of authentication than you
had intended. You need to follow my suggestion and either look at
User.Identity in the debugger, or write it out with Response.Write.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top