Solved

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

Posted on 2010-11-26
16
429 Views
Last Modified: 2013-12-17
 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
0
Comment
Question by:newjeep19
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
16 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34219458
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.
0
 

Author Comment

by:newjeep19
ID: 34219555
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
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34219592
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.
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 20

Expert Comment

by:BuggyCoder
ID: 34219597
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
:-)
0
 
LVL 4

Expert Comment

by:rstjean
ID: 34219651
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.
0
 

Author Comment

by:newjeep19
ID: 34219694
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;
            }
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34219733
Nothing happens meaning what? It doesn't hit the event handler, doesn't pick up the username, doesn't send the email?
0
 

Author Comment

by:newjeep19
ID: 34219754
Does not send the email and nor the even handler
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34219800
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.
0
 

Author Comment

by:newjeep19
ID: 34219834
OK I fixed the event handler issue still does not submit an email
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34219855
Have you stepped through with the debugger? Is it picking up the username as per your original question?
0
 

Author Comment

by:newjeep19
ID: 34219884
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;
            }
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34219897
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.
0
 

Author Comment

by:newjeep19
ID: 34219923
I hace check my email server and nothing is going through
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34219931
Well if it isn't throwing an exception then the message is going somewhere, and the server it is going to isn't complaining about it. Have you double checked your server address and port details for the SMTP Client, checked the sender and recipient addresses? Are you sure it isn't throwing an exception? May be worth checking the mail servers pending queue.
0
 

Author Comment

by:newjeep19
ID: 34241715
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;
            }
        }
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question