0

How to capture a user's Active Directory userPrinicpalName when they login with Windows authentication, and then compare that to a SQL Server table column in C#?

Any ideas would be much appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
NeoAer
  • 327
  • 3
  • 15
  • 1
    Hi and welcome to SO. Your question is extremely broad. What part(s) of this do you need help with? – Sean Lange Sep 05 '18 at 20:56
  • Kind of all of it to be honest sadly i'm new at coding. I have created a website that uses Windows Authentication. I want to take that authentication and pull more information such as the 'userPrincipalName' property from the user. With the userPrincipalName I want to compare it to a SQL Server Database column which hold other userPrincipalName and then with that comparison pull the rest of the data from that row to the website to be able to display for the user. My goal is to allow the user to view their own information in the SQL Server Database and allow them to update it. – NeoAer Sep 05 '18 at 21:06
  • I guess I can break this down one step at a time. How do I pull more information from the user once they use windows authentication to visit my site such as their AD property of 'userPrincipalName'. – NeoAer Sep 05 '18 at 21:08

1 Answers1

0

Check out this page to get the Windows username: How do I get the current username in .NET using C#?

From there you would need to write a sql query that would look up the username.

cmd.CommandText = $"SELECT* FROM table WHERE columnName='{WindowsIdentity.GetCurrent().Name}'"

You may need additional using statement to use WindowsIdentity. Add using System.Security.Principal to where this method is being used.

B Minster
  • 331
  • 3
  • 16