Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspdotnet_website_programming thread: System.InvalidCastException: Specified cast is not valid.


Message #1 by ashwinia@h... on Fri, 22 Nov 2002 15:22:27
Hi all,

I am getting the following error while casting the Context.User to 
SitePrincipal in VB.NET.

The code snippet is like this:
     Dim siteUser As CACI.WebModules.Accounts.Business.SitePrincipal
     siteUser = CType(Context.User, SitePrincipal)

The SitePrincipal is the same class as is there in the ASP.NET website 
programming book.

Any help would be greatly appreciated.
Ashwini
Message #2 by <kd@k...> on Fri, 22 Nov 2002 09:29:51 -0600
Here is how I did it  (plus some additional code that may help)......I hope
this helps....(it took me awhile to figure out too)

SitePrincipal currentPrincipal=new
SitePrincipal(Context.User.Identity.Name);

SiteIdentity currentIdentity=(SiteIdentity)currentPrincipal.Identity;

int householdID;

householdID=currentIdentity.HouseholdID;

Household updateHousehold = new Household(HouseholdID);

(I added householdD to the properties of the SiteIdentity)

----- Original Message -----

From: <ashwinia@h...>

> Hi all,
>
> I am getting the following error while casting the Context.User to
> SitePrincipal in VB.NET.
>
> The code snippet is like this:
>      Dim siteUser As CACI.WebModules.Accounts.Business.SitePrincipal
>      siteUser = CType(Context.User, SitePrincipal)
>
> The SitePrincipal is the same class as is there in the ASP.NET website
> programming book.

Message #3 by "Joshi, Sunit" <sjoshi@i...> on Fri, 22 Nov 2002 09:42:09 -0600
This is what I do. from the Submit_Click event in the Login Page:

protected Accounts.AccountsBusiness.SitePrincipal newUser;
protected Accounts.AccountsBusiness.SiteIdentity sid;

protected void btnSubmit_Click(object sender, System.EventArgs e)
{	
     // Clear out the existing user
	FormsAuthentication.SignOut();
	newUser = SitePrincipal.ValidateLogin(EmailAddress.Text.Trim(), Password.Text.Trim());			
	if (newUser == null)
	{
		LoginResult.Text = "Login failed for " + EmailAddress.Text;
		LoginResult.Visible = true;
	}
	else 
	{								
		sid = (SiteIdentity) newUser.Identity;
		sid.TimesHere = 1;
            // Store SitePrincipal for user in Session Object
		Session["User"] = newUser;				
		FormsAuthentication.RedirectFromLoginPage(EmailAddress.Text.Trim(), false);	
	}
}

Then in any page where I need it:

protected Accounts.AccountsBusiness.SitePrincipal sp;
protected Accounts.AccountsBusiness.SiteIdentity sid;

if(Context.User.Identity.IsAuthenticated && (Session["User"] != null))
{
   sp = (Skm.Accounts.AccountsBusiness.SitePrincipal)Session["User"];
   sid = (Skm.Accounts.AccountsBusiness.SiteIdentity) sp.Identity;
}
else
	Response.Redirect("/skm/Users/Login.aspx");


hth...

Sunit
-----Original Message-----
From: kd@k... [mailto:kd@k...]
Sent: Friday, November 22, 2002 9:30 AM
To: Website Programming with ASP.NET
Subject: [aspdotnet_website_programming] Re:
System.InvalidCastException: Specified cast is not valid.


Here is how I did it  (plus some additional code that may help)......I hope
this helps....(it took me awhile to figure out too)

SitePrincipal currentPrincipal=new
SitePrincipal(Context.User.Identity.Name);

SiteIdentity currentIdentity=(SiteIdentity)currentPrincipal.Identity;

int householdID;

householdID=currentIdentity.HouseholdID;

Household updateHousehold = new Household(HouseholdID);

(I added householdD to the properties of the SiteIdentity)

----- Original Message -----

From: <ashwinia@h...>

> Hi all,
>
> I am getting the following error while casting the Context.User to
> SitePrincipal in VB.NET.
>
> The code snippet is like this:
>      Dim siteUser As CACI.WebModules.Accounts.Business.SitePrincipal
>      siteUser = CType(Context.User, SitePrincipal)
>
> The SitePrincipal is the same class as is there in the ASP.NET website
> programming book.


Message #4 by ashwinia@h... on Fri, 22 Nov 2002 16:23:58
Sunit - Thank you very much for your suggestion but it is in C# and I need 
in VB.NET. For C#, I think using the Context.User will also work as is 
written in the Book.

The equivalent VB.NET code will help me in this situation.

Thanks - ashwini

> This is what I do. from the Submit_Click event in the Login Page:

protected Accounts.AccountsBusiness.SitePrincipal newUser;
protected Accounts.AccountsBusiness.SiteIdentity sid;

protected void btnSubmit_Click(object sender, System.EventArgs e)
{	
     // Clear out the existing user
	FormsAuthentication.SignOut();
	newUser = SitePrincipal.ValidateLogin(EmailAddress.Text.Trim(), 
Password.Text.Trim());			
	if (newUser == null)
	{
		LoginResult.Text = "Login failed for " + EmailAddress.Text;
		LoginResult.Visible = true;
	}
	else 
	{								
		sid = (SiteIdentity) newUser.Identity;
		sid.TimesHere = 1;
            // Store SitePrincipal for user in Session Object
		Session["User"] = newUser;				
		FormsAuthentication.RedirectFromLoginPage
(EmailAddress.Text.Trim(), false);	
	}
}

Then in any page where I need it:

protected Accounts.AccountsBusiness.SitePrincipal sp;
protected Accounts.AccountsBusiness.SiteIdentity sid;

if(Context.User.Identity.IsAuthenticated && (Session["User"] != null))
{
   sp = (Skm.Accounts.AccountsBusiness.SitePrincipal)Session["User"];
   sid = (Skm.Accounts.AccountsBusiness.SiteIdentity) sp.Identity;
}
else
	Response.Redirect("/skm/Users/Login.aspx");


hth...

Sunit
-----Original Message-----
From: kd@k... [mailto:kd@k...]
Sent: Friday, November 22, 2002 9:30 AM
To: Website Programming with ASP.NET
Subject: [aspdotnet_website_programming] Re:
System.InvalidCastException: Specified cast is not valid.


Here is how I did it  (plus some additional code that may help)......I hope
this helps....(it took me awhile to figure out too)

SitePrincipal currentPrincipal=new
SitePrincipal(Context.User.Identity.Name);

SiteIdentity currentIdentity=(SiteIdentity)currentPrincipal.Identity;

int householdID;

householdID=currentIdentity.HouseholdID;

Household updateHousehold = new Household(HouseholdID);

(I added householdD to the properties of the SiteIdentity)

----- Original Message -----

From: <ashwinia@h...>

> Hi all,
>
> I am getting the following error while casting the Context.User to
> SitePrincipal in VB.NET.
>
> The code snippet is like this:
>      Dim siteUser As CACI.WebModules.Accounts.Business.SitePrincipal
>      siteUser = CType(Context.User, SitePrincipal)
>
> The SitePrincipal is the same class as is there in the ASP.NET website
> programming book.



  Return to Index