ON local run successfully but error on IIS(same PC)

Posted by Rushighatage under ASP.NET on 3/20/2014 | Points: 10 | Views : 4772 | Status : [Member] | Replies : 2
when i run application directly it run successfully

clientMachineName =System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;


using this i get looged user name

but when i go through IIS it gives following error

Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'.

please Help



Thank u




Responses

Posted by: Durgammahesh22 on: 3/20/2014 [Member] Starter | Points: 25

Up
0
Down
Hi,

Try like this.

I think, you can get like this

--> Request.ServerVariables["REMOTE_ADDR"]
--> Request.ServerVariables["REMOTE_HOST"]
--> Request.ServerVariables["REMOTE_USER"]

But you need to enable Reverse DNS Lookup for you IIS.

or

string PCName = Dns.GetHostEntry(Request.ServerVariables["REMOTE_ADDR"]).HostName;

Thanks,
Mahesh


Thanks,
Mahesh

Rushighatage, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 3/20/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,
If your requirement is to get the logged in username then you can use the below code
   //Get the Logged in username

string clientMachineName = HttpContext.Current.User.Identity.Name;

This will give you the current user for the web application.

Please ensure that you have added the below namespace in your page
using System.Web;


Please Mark my Reply as Answer if it answers your question

Thanks,
A2H
My Blog

Rushighatage, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response