Solved

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

Posted on 2010-11-26
16
423 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
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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Log4Net custom Appender not being fired. 5 45
Hovering effect 9 29
Code enhancement 5 13
Hidden Field Value 10 37
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now