Forms Authentication not working on server

R

Russell

Hello there,

keywords so you can find this message: russell mccloy server forms
authentication InvalidCastException not working after login logon

We have an issue with Forms Authentication. I have a site using forms
authentications. It runs perfectly on 3 pcs and 2 servers but wont run
on our ISP's server.

After login the following error occurs:

[InvalidCastException: Specified cast is not valid.]
BNFL_EnergyUnit.rms.rmsUserControls.rmsMenu.setUpMenuBasedOnAccessLevel()
BNFL_EnergyUnit.rms.rmsUserControls.rmsMenu.Page_Load(Object
sender, EventArgs e)
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Control.LoadRecursive() +98
System.Web.UI.Control.LoadRecursive() +98
System.Web.UI.Page.ProcessRequestMain() +739

I have beed reading for hours and cant find anything.
Got to get this site live by tomorrow.
any help will be greatly appreciated.

RuSs
 
R

Raterus

Well, seeing the code of this method
BNFL_EnergyUnit.rms.rmsUserControls.rmsMenu.setUpMenuBasedOnAccessLevel()

and the specific line of code this error occurs on would be very helpful...
 
R

russell mccloy

ok.

The code that is failing is on most pages of my site and is:

energyCUSTOM_PRICIPAL currentLoggedInUser =
(energyCUSTOM_PRICIPAL)Context.User;

Basically I have created a more detailed object (energyCUSTOM) that
inherits IPrincipal

The reason I didnt send code is becuase this code works on all our
servers, it just wont work on our ISP so I thought you may know of some
IIS reason why forms authentication isnt working on one server but is on
other.

I cna give more info if required.

thanks
RuSs
 
K

Karl Seguin

if that's really the line that's failing...the only possibility is that
Context is null...

This to me is impossible if you are using this within a page 'cuz
Page.Context can't be null...my guess is that it's a nother line which is
bugged..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
R

Raterus

Have you tried comparing the object type that "context.user" is at the point of that code on a server that works vs. your ISP. It would be interesting to know if they are different... Are you doing any other code that plays around with the context?
 
R

russell mccloy

ok,

on my server....which works my context.User is of type:
BNFL_EnergyUnit.CODE.BLL.energyCUSTOM_PRICIPAL

but on the server it is:
System.Security.Principal.GenericPrincipal

something on the server is causing context.User to be a GenericPrincipal
but on my machine it allows me to convert context.user to my
energyCUSTOM_PRICIPAL

Following is my energyCUSTOM_PRICIPAL then inherits IPrincipal

using System;
using System.Security.Principal;
using BNFL_EnergyUnit.CODE.DAL;
using System.Collections;

namespace BNFL_EnergyUnit.CODE.BLL
{
//*********************************************************************
//
// CustomPrincipal Class
//
// The CustomPrincipal class implements the IPrincipal interface so it
// can be used in place of the GenericPrincipal object. Requirements
for
// implementing the IPrincipal interface include implementing the
// IIdentity interface and an implementation for IsInRole. The custom
// principal is attached to the current request in Global.asax in the
// Authenticate_Request event handler. The user's role is stored in
the
// custom principal object in the Global_AcquireRequestState event
handler.
//
//*********************************************************************

public class energyCUSTOM_PRICIPAL : IPrincipal
{
private int _UserID;
private int _GroupID = 0;

// Required to implement the IPrincipal interface.
protected IIdentity _Identity;

/// <summary>
/// creates an new instance of the energy unit custom principle which
is a session based object that stores the currently logged on user's
creds. for the current session.
/// </summary>
public energyCUSTOM_PRICIPAL() {}

/// <summary>
/// creates an new instance of the energy unit custom principle which
is a session based object that stores the currently logged on user's
creds. for the current session.
/// </summary>
public energyCUSTOM_PRICIPAL(IIdentity identity, int userID, int
groupID)
{
_Identity = identity;
_UserID = userID;
_GroupID = groupID;
}

// IIdentity property used to retrieve the Identity object attached to
// this principal.
public IIdentity Identity
{
get { return _Identity; }
set { _Identity = value; }
}

// The user's ID, created when the user was inserted into the database
public int UserID
{
get { return _UserID; }
set { _UserID = value; }
}

/// <summary>
/// the users access level editor / content manager / super admin
/// </summary>
public int GroupID
{
get { return _GroupID; }
set { _GroupID = value; }
}


//********************************************************************
*
//
// Checks to see if the current user is a member of AT LEAST ONE of
// the roles in the role string. Returns true if found, otherwise
false.
// role is a comma-delimited list of role IDs.
//
//********************************************************************
*

public bool IsInRole(string role)
{
string [] roleArray = role.Split(new char[] {','});

foreach (string r in roleArray)
{
if (_GroupID == int.Parse(r))
return true;
}
return false;
}

public bool checkIfUserCanEditThisPage(int userId, int pageId)
{
dataAccessLayer DBLayer = new dataAccessLayer();
return DBLayer.checkIfUserCanEditThisPage(userId, pageId);
}

public ArrayList getUserStreams(int userId, string stream)
{
dataAccessLayer DBLayer = new dataAccessLayer();
return DBLayer.getUserStreams(userId, stream);
}

public static void updateUsersLastLoginDate(int userId)
{
dataAccessLayer DBLayer = new dataAccessLayer();
DBLayer.updateUsersLastLoginDate(userId);
}

/*public ArrayList getUserPages(int userId)
{
dataAccessLayer DBLayer = new dataAccessLayer();
return DBLayer.getUserPages(userId);
}*/

}
}
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top