Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

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
0
newjeep19
Asked:
newjeep19
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
newjeep19Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
Independent Software Vendors: 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!

 
BuggyCoderCommented:
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
 
rstjeanCommented:
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
 
newjeep19Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
Nothing happens meaning what? It doesn't hit the event handler, doesn't pick up the username, doesn't send the email?
0
 
newjeep19Author Commented:
Does not send the email and nor the even handler
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
newjeep19Author Commented:
OK I fixed the event handler issue still does not submit an email
0
 
Carl TawnSystems and Integration DeveloperCommented:
Have you stepped through with the debugger? Is it picking up the username as per your original question?
0
 
newjeep19Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
newjeep19Author Commented:
I hace check my email server and nothing is going through
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
newjeep19Author Commented:
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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Tackle projects and never again get stuck behind a technical roadblock.
Join Now