Link to home
Start Free TrialLog in
Avatar of newjeep19
newjeep19Flag for United States of America

asked on

How do I authenticate a currently logged in user for my ASP.NET Web form

 I need to get the name of the currently logged in user, when they arre accrssing my web form using NT Security. I then need to take that user name and append it to my company's domain name. I am using C#.NET and ASP.NET....Please help
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

If you're using Windows authentication (which it sounds like you are) then you simply use:

string name = HttpContext.Current.User.Identity.Name;

Open in new window


Which will return DOMAIN\username so you shouldn't need to append it yourself.
Avatar of newjeep19

ASKER

I am using NT Security. What I am looking to do is when the user is accessing my webpage I wan't NT security to get the user name from when they logged into their computer (this form will live on my companies intranet server). I need to then append that user name to my compaines domain name so that when they click on the submit button the form that they were filling out is submitted to an email addrss. This form will only be used by employees of the company. I tried using Active Directory however but that does not work do to premission levels of employees. In a nut shell when an employee logs onto their computer and then access the webpage I need to get their username (that they entered in when they signed onto their computer) and capture that user name and append to the form field to submit an email. I do not want to take them to a log on screen. I am using ASP.NET and C#.
Thanks for your help
And as I have already said in my last post you use the Identity object which retrieves the Windows Login name of the user. That's the whole point of Windows authentication; if the user is logged into the domain then they are automatically logged into your site and your retrieve the name of the user using the Identity object as demonstrated in my previous post.
ASP.NET is a specialized host in .net framework with special permissions. it cannot send to server the logged in NT User name, the only possible solution is LDAP. but for that you need to have one user which can access active directory in read only mode. only then you will be able to create a system where in your user need not to login to any form, his/her Network authentication will serve as all ASP.NET forms authentication. Given below are few links to help you:-

http://www.beansoftware.com/ASP.NET-Tutorials/Forms-Authentication-Active-Directory.aspx
http://www.codeproject.com/KB/system/NET_LDAP_Authentication.aspx

hope it helps
:-)
Make sure that you have that site in your "intranet" list.  If you don't then it won't pass the credentials across automatically.  Just means they will get an authentication box.
I understand but when I submit the request nothing happends:
Code block:
  protected void btnSend_Click(object sender, EventArgs e)
        {
            // Declare a string variable to hold senders email
            // and assign it the return value of the GetADUserMail() method
            string senderAddress = HttpContext.Current.User.Identity.Name;

            // If sender is null, then user not found or has no email address
            if (senderAddress == null)
            {
                Label1.Text = "Your user account was not found, or you have no email address - message not sent.";
                return;
            }

            try
            {

                MailMessage mail = new MailMessage();
                mail.From = new MailAddress(senderAddress);
                mail.To.Add("company@domain.net");                      // put To: address here
                mail.Subject = "IT Account Request Form";            // put subject here      
                mail.Body = TextBox9.Text;                          // put body of email here

                // put smtp server you will use here
                SmtpClient smtp = new SmtpClient("email server ip");
                smtp.Send(mail);

                Label1.Text = "Your message was successfully sent.";
            }
            catch (Exception ex)
            {
                Label1.Text = "An error occurred sending your e-mail, the error is:\r\n" + ex.Message;
            }
Nothing happens meaning what? It doesn't hit the event handler, doesn't pick up the username, doesn't send the email?
Does not send the email and nor the even handler
Well if it isn't hitting the event handler than it isn't hooked up properly. Switch to design view, double-click the button in question and see if it takes you to the code you have defined.
OK I fixed the event handler issue still does not submit an email
Have you stepped through with the debugger? Is it picking up the username as per your original question?
Yes, I have stepped through the debugger and so email is being sent. I do not get a message stating email has or has not been sent.  as shown in my try / catch block
protected void btnSend_Click(object sender, EventArgs e)
        {
            // Declare a string variable to hold senders email
            string senderAddress = HttpContext.Current.User.Identity.Name;

            // If sender is null, then user not found or has no email address
            if (senderAddress == null)
            {
                Label1.Text = "Your user account was not found, or you have no email address - message not sent.";
                return;
            }

            try
            {

                MailMessage mail = new MailMessage();
                mail.From = new MailAddress(senderAddress);
                mail.To.Add("company@domain.net");                      // put To: address here
                mail.Subject = "IT Account Request Form";            // put subject here      
                mail.Body = TextBox9.Text;                          // put body of email here

                // put smtp server you will use here
                SmtpClient smtp = new SmtpClient("email server ip");
                smtp.Send(mail);

                Label1.Text = "Your message was successfully sent.";
            }
            catch (Exception ex)
            {
                Label1.Text = "An error occurred sending your e-mail, the error is:\r\n" + ex.Message;
            }
If it is getting as far as sending the message to your mail server then that is where you need to look next. Mail messages are generally a fire-and-forget operation, unless the server returns an specific error.
I hace check my email server and nothing is going through
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I still need to append the user name to the domain name. This application is an internal app. In other words it is going to be used only by internal customers i.e. employess. So, instead of using Active Directory to get the user name. I found it eaiser to use Windows Authentication WA. So, the app is authenicating the user name from  single sign on and the removing the \\ before there user name. The next step is to take the user name that has already been authenticated using WA and that has had the \\ split away from the user name and appending that user name to the company's domain name i.e. company.net to the from field in the email button click event. I know that the email code is functional when I put only my company email address into the from field.
Code:
 protected void btnSend_Click(object sender, EventArgs e)
        {
            string email = String.Empty;
            string username = HttpContext.Current.User.Identity.Name.Split('\\')[1];
            MailMessage mail = new MailMessage();

            try
            {


                mail.From = new MailAddress(username +company@domain.net);
                mail.To.Add("company@domain.net");                      // put To: address here
                mail.Subject = "IT Account Request Form";            // put subject here      
                mail.Body = TextBox9.Text;                          // put body of email here

                // put smtp server you will use here
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "ip address of email server";
                smtp.Port = 25;
                smtp.Send(mail);

                Label1.Text = "Your message was successfully sent.";
            }
            catch (Exception ex)
            {
                Label1.Text = "An error occurred sending your e-mail, the error is:\r\n" + ex.Message;
            }
        }