leora
leora

Reputation: 196539

capture windows username in asp.net mvc request

i have an intranet app asp.net mvc site. is there anyway to capture the windows nt login from the user wihtout having a full login system in the site. i dont want permissioning but i would like to do some logging on the server to track requests, etc . .

Upvotes: 9

Views: 34166

Answers (7)

Samet DOĞAN
Samet DOĞAN

Reputation: 1

var MachineName = HttpContext.Server.MachineName;
var BrowserName = HttpContext.Request.Browser;
var Ip Address = HttpContext.UserHostAddress;

You can get information with these codes.

Upvotes: 0

Admiral Land
Admiral Land

Reputation: 2492

You can try to fetch user's name with the following piece of code:

var userName = HttpContext.User.Identity.Name;

User.Identity is automatically populated if you use any authentication (example: cookie based, etc)

Upvotes: 0

Diego
Diego

Reputation: 2362

try this.

 var user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

it returns Domail/User

Do not forget to put this in web.Config

<identity impersonate="true"/> 

Upvotes: 3

James Toomey
James Toomey

Reputation: 6082

Go to the Web.Config file for your application (be sure it's the main one, not the Web.Config under Views), remark out <authentication mode="Forms"> if you see it, then add lines like this:

<authentication mode="Windows"/>
<identity impersonate="true"/>
<authorization>
  <allow roles="DOMAIN\PersonnelGroup" />
  <allow users="DOMAIN\jdoe"/>
  <deny users="*"/>
</authorization>

You don't have to include the <authorization> section but it's useful if you want to allow access for certain groups and users, and deny everyone else. When you use IE, the credentials get passed along automatically. You can test this by printing the username in your view:

@HttpContext.Current.User.Identity.Name

If you open your site using Firefox or any other browser besides IE, it will prompt you for the username and password because it's not automatically passing along the credentials (although apparently you can get Firefox to pass along the credentials, as Dan Diplo mentions above).

If you want to pass these credentials along to another server, such as a SQLServer, it becomes more of a headache from my experience, because you run into a double hop issue. The only workaround I know of is to host IIS and SqlServer on the same server, or have a login for your intranet so you have the username and password to pass along to SQLServer.

Upvotes: 2

Dan Diplo
Dan Diplo

Reputation: 25339

You can use HttpContext.Current.User.Identity.Name if you set up the site to use Windows Authentication. Many browsers will pass the username on transparently.

Upvotes: 15

IanT8
IanT8

Reputation: 2217

What version of Windows is this mvc app running on?

You could try the following:

  • Use IIS Manager to disable anonymous access to the site, and make sure digest and/or windows authentication is enabled.
  • Change the mvc app's web.config to use Windows Authentication
  • Access the login name in the controller using User.Identity.Name.

Would that do?

Upvotes: 1

blowdart
blowdart

Reputation: 56500

If you set IIS to use Windows Authentication and the site is in the Intranet Zone for your clients then the logins will automatically happen without any prompts at all.

Upvotes: 0

Related Questions