1

I've been getting an empty string whenever I try to retrieve the logged in username in my controller. When I first created the app, I selected 'Internet application' template. I also deleted the default account controller, account models and _logon views as I didn't need them. I'm using my own styling, so I removed site.css from the project as well.

After playing around with the web.config for a while, I figured out that "User.Identity.Name" actually works if I change the authentication mode in web.config to windows. If I leave it on 'forms' authentication mode, I only get an empty string whenever I try to get the username.

Recently, I changed the authentication mode to Windows and used User.Identity.Name in one of my controllers to get the user name, but whenever I run the app, I get an error on the browser, stating "localhost/Account/LogOn/..." is not found. (not directing to my usual view) ( I didn't make any changes in Global.asax either.)

If I change the authentication mode back to forms, my view works fine, but I don't get to see the user name (just an empty string). Is there anyway I can find a way around this problem. Is there anything wrong with routing or something ? I can't afford to start over again using "intranet Application" template.

I'm a beginner in MVC, so any help would be greatly appreciated.

Thanks

Tippy20
  • 21
  • 1
  • I've just started to play around with MVC. Have not started using any log-in facility yet. So far, my app just queries some data and displays them on the view. I just thought, it'd be nice to display the user-name on the browser. I don't think we need all those account models and controllers to use just "User.Identity.Name" Thanks for the reply though. will be careful next time before deleting any models/controllers created from the template. :) – Tippy20 Sep 06 '12 at 19:16
  • 1
    In addition to the web.config, I believe there is a setting on the IIS server where you can enable windows authentication – Nigel Findlater Sep 06 '12 at 19:19
  • 1
    I am confused - what exactly are you trying to achieve? MVC has different modes for authentication, forms and windows. If you are not using either of them, then I am not sure what exact is your objective here. – sarsnake Sep 06 '12 at 19:22
  • I'm pretty new to web development. Obviously, going through a steep learning curve. I didn't think of authentication and stuff when I first created the app last week. My only objective that time was to query some data and display them on the view. Now I thought about displaying the "windows" username on the view as well. Didn't know that Logon view and account controller had that much of an impact even to get the username. Thanks – Tippy20 Sep 06 '12 at 19:38

3 Answers3

2

If you are using asp.net mvc, try System.Environment.UserName

inside your web.config, use

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

The error you are getting is because you removed a logon view it still must be refered to somewhere within your application, so if you don't need the logon view, make sure you remove all refences to it from you code.

sarsnake
  • 26,667
  • 58
  • 180
  • 286
  • User.Identity.Name is the correct way to access the user name, but you have to login first. – slfan Sep 06 '12 at 19:24
  • My solution assumes that he wanted the Windows authentication because he removed the logon view. – sarsnake Sep 06 '12 at 19:40
  • see this http://stackoverflow.com/questions/8841816/system-web-httpcontext-current-user-identity-name-vs-system-environment-username for more info – sarsnake Sep 06 '12 at 19:41
  • "System.Environment.UserName" works for me. Thanks for the help. – Tippy20 Sep 06 '12 at 20:03
  • It's basically the same. User.Identity.Name works independent of the authentication mode. System.Environment.Username always returns the windows user name, even if anonymous access is configured. It depends what you need. – slfan Sep 06 '12 at 20:08
  • @slfan, System.Environment.Username will indeed return the name always, but it may be different than you'd expect depending on the authentication mode your app is running under. It could be something like NETWORK_SERVICE. If you want the actual user name such as JOHN.SMITH, you'd have to make sure that your authentication mode is set properly. – sarsnake Sep 07 '12 at 18:29
  • @Tippy20, glad I could help, please accept the answer if it worked for you. – sarsnake Sep 07 '12 at 18:30
0

Internet applications works per default with forms authentication. The purpose of Windows authentication is for intranets, where the web application runs under a windows user. Then the authentication works "automatically".

If you want to have an internet application with registered users, you should put the following configuration in web.config:

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>

Then you need an AccountController (was there in the default template and you have probably deleted) and a Logon action within the controller. You also need all the views of the account controller (logon, register, change password etc.). The best would be you create a new internet application and check everything that's there. Just copy the stuff you need into your application.

You also need a user database. The default uses an express database with standard tables and stored procedures. If needed, you can use your own tables, then you have to rewrite the methods in the account controller or write your own membership provider.

The reason why you don't see a user name is because you have no login.

EDIT: If you want to display the Windows user name, you should set the authentication mode to windows (or just delete the authentication section, as it is the default). Then you can access the user name. But you will have to delete the tag.

<authentication mode="Windows" /> 
slfan
  • 8,950
  • 115
  • 65
  • 78
  • Thanks. Actually, It's a very simple app that I created to learn MVC basics. Assume that someone created an application using "internet application" template, and later decided to make it an "intranet" application. How to do that ? (All we need is to make the authentication work Automatically, as you've mentioned) I am sure there aren't any Account models, or logon views present when an mvc3 app is created using Intranet appication template. The only change I found so far is in the Web.Config. So, how to change an existing "internet" app to an "intranet" app without recreation ? Thanks – Tippy20 Sep 06 '12 at 19:50
  • Just ask the correct question in StackOverflow ;-). It's quite easy: all you need is the correct configuration in web.config. You can delete the Account Controller and the corresponding views. You might want to use different roles/groups, then you could add RequiresRole attribute on the controllers or actions where you need special authorization. – slfan Sep 06 '12 at 19:53
  • Yeah, if I change the authentication mode, I have no problems retrieving the username (checked it using breakpoints on the controller), but I get error on the view stating something like "localhost/Views/Account/LogOn/..." is not found. Is it anything to do with the routing ? (Funny thing is we don't even have any LogOn views on an Intranet Application). (I'm thinking it is still looking for the view returned by accounts controller using forms authentication) – Tippy20 Sep 06 '12 at 19:56
  • K. I'll double check the configuration. Thanks – Tippy20 Sep 06 '12 at 19:58
  • did you really DELETE the forms tag in the web.config? Or do you click on a login action which points to Views/Account/LogOn"? – slfan Sep 06 '12 at 19:59
  • I replaced with It works now though, using System.Environment.UserName - Thanks – Tippy20 Sep 06 '12 at 20:04
  • It might work, but this change didn't solve your real problem. There must be something else wrong if the app redirectes to the logon action. – slfan Sep 06 '12 at 20:09
0

Sounds like you aren't even authenticating first, so there is no username:

@if (Request.IsAuthenticated) {
    <span>@User.Identity.Name</span>
}
else {
    <span>You aren't authenticated</span>
}
naspinski
  • 34,020
  • 36
  • 111
  • 167