User.Identity.Name returns nothing / NULL

G

Guest

Hi
I have created two pages, the login.aspx and the formA.aspx, and each of
this has a corresponding *.cs files for code separation

the scenario is, I seems to be able to login / authenticated because
formA.aspx got loaded. But when I added the following lines
Label1.Text = HttpContext.Current.User.Identity.Name;

to the Page_Load event under FormA.cs, the control Label1 simply not showing
anything!

Do I need to do anything to get the "User.Identity.Name" set? (in config
files and / or after successful login)?
What is the problem here, please?

TIA
____
username and password collected by login.aspx is authenticated against the
AD with LDAP using the following code
//
DirectoryEntry entry = new DirectoryEntry(strPath, uName, pwd);
try
{
// Bind to the native object to force authentication to happen
Object obj = entry.NativeObject;
}
catch (Exception ex)
{
throw new Exception("User authentication failed: " +
ex.Message);
}

Server.Transfer("formA.aspx");
_______
formA.aspx contains a form for user to fill out some information.
--
 
D

Dominick Baier [DevelopMentor]

Hello dl,

makes sure you are really authenticated - IIS is LBD (lazy by default). Go
the the directory security tab and clear "anonymous login" for the virtual
directory.
 
D

Dominick Baier [DevelopMentor]

Hello dl,

ah - ok - if you are doing custom authentication - you have to construct
an IPrincipal object yourself -

have a look at GenericPrincipal and AuthenticateRequest in the docs.

Basically you have to construct that Principal object in the AuthenticateRequest
event and attach it to Context.User, like

Context.User = new GenericPrincipal(new GenericIdentity(..), roles);
 
G

Guest

Hi Dominick
I think this is the problem, and I found the following link under MS
http://msdn.microsoft.com/library/d...ve_directory_authentication_from_ASP__Net.asp
has an example using code behind with VS.Net, the problem is I am only using
Web Matrix / VWD 2005 Express. I am trying to put the Global.asax code in a
class using <@page ... inherits=...>, but there is one line I am having
problem with, can you help, please?

HttpCookie authCookie = Context.Request.Cookies[cookieName];

on compiling the class, I am getting the following error
The type or namespace "Context" could not be found (are you missing a using
directive or an assembly reference?)

by the way, can you give me the link to the docs on AuthenticateRequest /
GenericPricipal?
Is there any other good example on Form authentication along with the
required Global.asax file?

TIA
 
D

Dominick Baier [DevelopMentor]

Hello dl,

there is a sample on my blog which shows you how do this:
http://www.leastprivilege.com/PermaLink.aspx?guid=b0e51388-71d1-4a6f-98d0-bc8cfbec4c3a

btw - you can always get a context by calling:

HttpContext context = HttpContext.Current;

feel free to ask if you have any questions

HTH
---------------------------------------
Dominick Baier - DevelopMentor
http://www.leastprivilege.com
Hi Dominick

I think this is the problem, and I found the following link under MS

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sds/s
ds/active_directory_authentication_from_ASP__Net.asp

has an example using code behind with VS.Net, the problem is I am only
using

Web Matrix / VWD 2005 Express. I am trying to put the Global.asax
code in a

class using <@page ... inherits=...>, but there is one line I am
having

problem with, can you help, please?

HttpCookie authCookie = Context.Request.Cookies[cookieName];

on compiling the class, I am getting the following error
The type or namespace "Context" could not be found (are you missing a
using
directive or an assembly reference?)
by the way, can you give me the link to the docs on
AuthenticateRequest /
GenericPricipal?
Is there any other good example on Form authentication along with the
required Global.asax file?
TIA

Hello dl,

ah - ok - if you are doing custom authentication - you have to
construct an IPrincipal object yourself -

have a look at GenericPrincipal and AuthenticateRequest in the docs.

Basically you have to construct that Principal object in the
AuthenticateRequest

event and attach it to Context.User, like

Context.User = new GenericPrincipal(new GenericIdentity(..), roles);
 
G

Guest

Hi Dominick
Thankyou very much.

In fact, I just experience a problem with the formA.aspx / formA.cs and post
another thread on newsgroup microsoft.public.dotnet.framework.aspnet, wonder
if you spare some time to take a look and advice on that also ?!

TIA

Dominick Baier said:
Hello dl,

there is a sample on my blog which shows you how do this:
http://www.leastprivilege.com/PermaLink.aspx?guid=b0e51388-71d1-4a6f-98d0-bc8cfbec4c3a

btw - you can always get a context by calling:

HttpContext context = HttpContext.Current;

feel free to ask if you have any questions

HTH
---------------------------------------
Dominick Baier - DevelopMentor
http://www.leastprivilege.com
Hi Dominick

I think this is the problem, and I found the following link under MS

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sds/s
ds/active_directory_authentication_from_ASP__Net.asp

has an example using code behind with VS.Net, the problem is I am only
using

Web Matrix / VWD 2005 Express. I am trying to put the Global.asax
code in a

class using <@page ... inherits=...>, but there is one line I am
having

problem with, can you help, please?

HttpCookie authCookie = Context.Request.Cookies[cookieName];

on compiling the class, I am getting the following error
The type or namespace "Context" could not be found (are you missing a
using
directive or an assembly reference?)
by the way, can you give me the link to the docs on
AuthenticateRequest /
GenericPricipal?
Is there any other good example on Form authentication along with the
required Global.asax file?
TIA

Hello dl,

ah - ok - if you are doing custom authentication - you have to
construct an IPrincipal object yourself -

have a look at GenericPrincipal and AuthenticateRequest in the docs.

Basically you have to construct that Principal object in the
AuthenticateRequest

event and attach it to Context.User, like

Context.User = new GenericPrincipal(new GenericIdentity(..), roles);

---------------------------------------
Dominick Baier - DevelopMentor
http://www.leastprivilege.com
Hi
I have created two pages, the login.aspx and the formA.aspx, and
each
of
this has a corresponding *.cs files for code separation
the scenario is, I seems to be able to login / authenticated because
formA.aspx got loaded. But when I added the following lines
Label1.Text = HttpContext.Current.User.Identity.Name;
to the Page_Load event under FormA.cs, the control Label1 simply not
showing anything!
Do I need to do anything to get the "User.Identity.Name" set? (in
config
files and / or after successful login)?
What is the problem here, please?
TIA
____
username and password collected by login.aspx is authenticated
against
the
AD with LDAP using the following code
//
DirectoryEntry entry = new DirectoryEntry(strPath, uName,
pwd);
try
{
// Bind to the native object to force authentication to
happen
Object obj = entry.NativeObject;
}
catch (Exception ex)
{
throw new Exception("User authentication failed: " +
ex.Message);
}
Server.Transfer("formA.aspx");
_______
formA.aspx contains a form for user to fill out some information.
 

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,233
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top