By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,666 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,666 IT Pros & Developers. It's quick & easy.

how can User.Identity.Name be empty?

P: n/a
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
Mar 2 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"CharlesA" <Ch******@discussions.microsoft.comwrote in message
news:17**********************************@microsof t.com...
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

Mar 2 '07 #2

P: n/a
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
Mar 2 '07 #3

P: n/a
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).

"CharlesA" <Ch******@discussions.microsoft.coma écrit dans le message de
news: E7**********************************@microsoft.com...
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

Mar 2 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.