How to get the Windows User Name of the logged in user in ASP.Net, IIS

Hello There!

So, today i’ll speak  about how you can get the User ID of the current user who has logged into the Web Application.

Lets consider the Web Application is hosted in IIS.

Here, I am avoiding all the boiler plate code to create and set up your project and host into the IIS.

There are three ways to get the logged in User ID in ASP.Net.

FIRST WAY

<authentication mode=”Windows”/>

You can add the authentication tag into the <system.web> tag in the Web.config file of your project.

Enable the Anonymous access in IIS. You will find this setting in your Application under the Authentication Option.

Authentication.png EnableAnonymousAuthentication.png

Write the below lines of code in your application CS or VB file.

  1. System.Environment.UserName .. Will give you the Computer Name
  2. Page.User.Identity.Name .. Will give you Blank
  3. System.Security.Principal.WindowsIdentity.GetCurrent().Name .. Will give you the Computer Name

SECOND WAY

<authentication mode=”Windows”/>

You can add the authentication tag into the <system.web> tag in the Web.config file of your project.

Disable the Anonymous access & Enable Windows Authentication in IIS. You will find this setting in your Application under the Authentication Option.

Authentication.pngEnableWindowsAuthentication.png

After setting the Application to enable Windows Authentication, you will have to migrate your application to a Classic .NET AppPool and enable the  Integrated Pipeline for the Classic .Net AppPool inside the IIS.

AppPool.png

Write the below lines of code in your application CS or VB file.

  1. System.Environment.UserName .. Will give you ASPNET. i.e the account that is used to run the Asp.Net service.
  2. Page.User.Identity.Name .. Will give you Domain\Windows Account Name
  3. System.Security.Principal.WindowsIdentity.GetCurrent().Name .. Will give you Computer Name\ASPNET

 

THIRD WAY

<authentication mode=”Windows”/> <identity impersonate =”true”/>

You can add the above tags into the <system.web> tag in the Web.config file of your project.

Disable the Anonymous access & Enable Windows Authentication in IIS

Authentication.pngEnableWindowsAuthentication.png

After setting the Application to enable Windows Authentication, you will have to migrate your application to a Classic .NET AppPool and enable the  Integrated Pipeline for the Classic .Net AppPool inside the IIS.

AppPool.png

Write the below lines of code in your application CS or VB file.

  1. System.Environment.UserName .. Will give you Windows Account Name
  2. Page.User.Identity.Name .. Will give you Domain\Windows Account Name
  3. System.Security.Principal.WindowsIdentity.GetCurrent().Name .. Will give you Domain\Windows Account Name

* Browser will ask you for the user name and password if the Windows Authentication is enabled.

That’s all you have to do to fetch the Windows User ID in ASP .Net IIS environment.

Cheers!

Rohant K.

 

Advertisements

Using Microsoft Exchange Web Service – EWS

Hello There,

We do have requirements to integrate your application with the eMail, either by sending them or by reading them (Yes! – it is possible), for this purpose, Microsoft has a cool new protocol called as the Exchange Web Services.

If you have an eMail Exchange Server 2007 and above already set up, then it is a cake walk to integrate your application with the Exchange Server eMail using EWS.

NOTE: At the time of writing this article EWS API 2.2 was the latest version.

NOTE: I am using VB .Net for this example

So lets get started!

Firstly, you’ll need to download the EWS API from the Microsoft MSDN website, using the below URL and follow the installation procedure mentioned there:

https://www.microsoft.com/en-us/download/details.aspx?id=42951

After installing the EWS API 2.2, you must now turn on your Visual Studio environment and create a new Project:

VS1.pngnewProject.png

Click ‘Ok’ to create a new project.

Now lets turn our focus on the steps to reference the newly installed Exchange Web Service. We will have to manually reference them into our project. To do this:

Click the add reference menu item by right clicking the project > Add > Reference

reference

A Reference Manager window opens upReferenceManager.png

Click Browse on the left pane and then click Browse button again from the Footer

It will open up the Windows Explorer. Navigate to the below path, which ideally the default path where the Exchange Web Services are installed.

 

C:\Program Files\Microsoft\Exchange\Web Services\2.2

reference2.png

There are two dlls that you will have to import into your project. Click Add after you select them both.

reference3.png

You need to keep these information handy before you follow the code below:

  1. Your Microsoft Exchange Server version (For example Exchange Server 2007 or Exchange Server 2010 etc.)
  2. eMail ID, Password and the Domain Name that you will be using to send the eMail from
  3. URL of your exchange web service. Ideally, it is the exchange server name. You can find your exchange server name, by going into the Account Settings. The highlighted area in the image below is the server name. So the URL of the exchange web service would be https://<Server Name>/EWS/Exchange.asmx . Clicking on the link the web service page of the Exchange should open up. It will ask you for your user name and password.
  4. AccountInfo1.png
  5. An HTML File, for you to send an eMail (You can even send a plan text eMail.)

Now lets send an eMail using EWS!

Follow the code block below:


' We create an object of the Exchange Server. 
' In the argument, I specify the Exchange Server Version. Mine is 2010.
Dim service As New ExchangeService(ExchangeVersion.Exchange2010_SP2)
service.Credentials = New WebCredentials(<eMail ID of the Sender>, <Password of the sender>, <Domain Name under which your Exchange Server resides>)

' This is your Exchange Web Service URL
service.Url = New Uri("https://<Server Name>/EWS/Exchange.asmx")

' Set up the recepients eMail IDs
Dim oEmail As New EmailMessage(service)
oEmail.ToRecipients.Add(<Recipient EMail ID>)

' Read the HTML File and store it in a String.
 Dim sHtml As String = System.IO.File.ReadAllText("C:\index.html")

' EMail Subject, Body Block

 oEmail.Subject = "SUBJECT"
 oEmail.Body = New MessageBody(sHtml)
 oEmail.Body.BodyType = BodyType.HTML
 oEmail.Send()

' To trace the request and responses while using EWS. It helps to find out where your service is wrong.
 service.TraceEnabled = True
 service.TraceFlags = TraceFlags.All

and thats it!, this is how you can send an eMail using Microsoft Exchange Service API 2.2 web service.

You can extend the capability further, by reading the eMail messages from the inbox using the Exchange Web Service.

Let me know your comments!

-Cheers!

Rohant K.