how can User.Identity.Name be empty?

G

Guest

hi folks,

I've got XP pro service pack 2 VS 2005 TSE with ASP.net 2.0 and C#

I'm doing an example from Stephen Walther's so far excellent book and he
has a whole load of stuff to add the current user to a role like so:

<script runat="server">

void Page_Load()
{
if (!Page.IsPostBack)
{
foreach (ListItem item in cblSelectRoles.Items)
if (!Roles.RoleExists(item.Text))
{
Roles.CreateRole(item.Text);
Roles.AddUserToRole(User.Identity.Name, item.Text);
}
}
}

protected void btnSelect_Click(object sender, EventArgs e)
{
foreach (ListItem item in cblSelectRoles.Items)
{
if (item.Selected)
{
if (!User.IsInRole(item.Text))
Roles.AddUserToRole(User.Identity.Name, item.Text);
}
else
{
if (User.IsInRole(item.Text))
Roles.RemoveUserFromRole(User.Identity.Name, item.Text);
}
}
Response.Redirect(Request.Path);
}

void Page_PreRender()
{
foreach (ListItem item in cblSelectRoles.Items)
item.Selected = User.IsInRole(item.Text);
}
</script>



but two things happens
firstly it tells me that there is no overload for
Roles.AddUserToRole(User.Identity.Name, item.Text);

that takes only a string as first argument, so it has to be an array.

I create an array and populate it with User.identity.Name but then it turns
out that in the locals window that User.Identity.Name is empty (i.e "") how
can this be?

regards and many thanks in advance
CharlesA
 
M

Mark Fitzpatrick

I don't even know where to begin. First, you are correct,
Roles.AddUserToRole does take two strings.

The User.Identity.Name will be empty if the user is not logged in. You may
want to check and ensure first that the Use.IsAuthenticated is true.

Something else to keep in mind, when you perform the following:
User.IsInRole(item.Text); This may only grab the roles that the user had
when they logged in as they are often stored in a role cookie upon login. I
often use the Roles.IsUserInRole(username,role) when i need to be absolutely
sure.
 
G

Guest

so no user is logged in, I see!

interestingly and very bizarrely when I was using the Roles.AddUserToRoles
method the intellisense had no overload, it insisted that i needed a
(string[], string) parameter call, but when I looked in the object browser it
has 4 incl a (string, string) one.

thanks mark, I at least have some pointer to going forward now
Regards
CharlesA
 
P

Patrice

I would try to fully qualify. It looks like you could perhaps have another
Roles object that comes in scope ???

Alos what does it give at compilte time (i.e. is this just an intellisense
problem).
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top