Click here to Skip to main content
15,887,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone... I have a Windows Authentication screen (WinLogin.xaml) and it uses an ADHelper.cs the validate an account. That works just great, but for some reason, I cannot get the the system to pass the authenticated user's DISPLAY NAME back to the previous screen.

Basically...

WinLogin.xaml - enter username, password and press Login -- if OK, go to form1 else msgbox.
Form1.xaml - enter username's DISPLAY NAME from Winlogin.xaml to TBoxPharmSign.Text;

ADHelper.cs

C#
using System.DirectoryServices;

namespace BizLogic
{
    public class AdHelper
    {
        #region AuthenticateUser Method

        public bool AuthenticateUser(string domainName, string userName, string password)
        {
            var ret = false;
            try
            {
           var de = new DirectoryEntry("LDAP://" + domainName, userName, password);
           var dsearch = new DirectorySearcher(de);
               dsearch.FindOne();
                ret = true;
            }
            catch
            {
                ret = false;
            }
            return ret;
        }
        #endregion
    }
}


WinLogin.cs

C#
using System.Windows;
using BizLogic;

namespace Forms
{
    public partial class WinLogin
    {

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TxtDomain.Text = Environment.UserDomainName.ToLower();
        }

        private void WinLoginLogin_Click(object sender, RoutedEventArgs e)
        {
            var ad = new AdHelper();

            if (ad.AuthenticateUser(TxtDomain.Text,
                TxtUserName.Text, TxtPassword.Password))
            {
                    DialogResult = true;
            }
            else
                MessageBox.Show("Unable to Authenticate Using the Supplied Credentials");
        }
    }
}


Finally, the click action where the help is needed on Form1.xaml

C#
private void eSign_Click(object sender, RoutedEventArgs e)
{
    //added for winlogin instance
    var win = new WinLogin {Owner = this};

    win.ShowDialog();
    if (!win.DialogResult.HasValue || !win.DialogResult.Value)
    {
        Close();
    }
    else
    {


        TBoxPharmSign.Text = "AD Display Name From WinLogin.xaml - "
    }

    Log.Info("Cor551240415V1.EmployeeSearch_Click() called into existence for employee:");
}
Posted
Updated 1-Jun-15 12:17pm
v2
Comments
Mathi Mani 1-Jun-15 18:38pm    
From where are you getting the Display name? Where are you setting value to it? The code does not have one.
Derek Kennard 1-Jun-15 19:12pm    
That's what I'm looking for help on. I can't figure out how to set it.
Mathi Mani 1-Jun-15 19:19pm    
Where do you keep the details of user's display name? Are you planning to get them from Environment or from some other data source?
Derek Kennard 1-Jun-15 19:22pm    
I would like to get them from Active Directory

I answered my own question. I added a public void to WinLogin.cs
C#
public void ReturnNames()
{
    FullName = TxtUserName.Text;
    Domain = TxtDomain.Text;
}


and in Form1

C#
//added for winlogin instance
            var win = new WinLogin { Owner = this };

            win.ShowDialog();
            if (!win.DialogResult.HasValue || !win.DialogResult.Value)
                Close();
            else
                win.ReturnNames();
                TBoxPharmSign.Text = GetUserFullName(win.Domain,win.FullName);
        }


And also in Form1

C#
public static string GetUserFullName(string domain, string userName)
{
    DirectoryEntry userEntry = new DirectoryEntry("WinNT://" + domain + "/" + userName + ",User");
    return (string)userEntry.Properties["fullname"].Value;
}


in a nutshell, WinLogin.cs - ReturnNames() sets FullName and Domain equal to the values the user inputs on the screen.
Form1 builds the query to AD using GetUserFullName()
The eSign_Click in Form1 runs WinLogin - ReturnNames() and sets the textbox to the returned values from GetUserFullName()... :)
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900