MVC3 + How to get the current login name

I am new to MVC and actually new to web development. I have about 7 years of development experience, but in services, database, object models, etc., mainly medium-term and internal. I am trying to learn ASP.NET and decided to create a site using MVC3 for a personal site for myself. I will post this from my account on dotnet-hosts.com. Here is my question ... I do not have a domain and I will use the built-in membership provider. I noticed in the automatic generated code that was created when I added the project template, that in the AccountController in the ChangePassword ( ChangePasswordModel model) method is this line of code ...

 MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */); 

My question is specifically around User.Identity.Name , it looks like it will return a Windows username exactly like Environment.UserName . The Visual Studio template that I used is (Mobile Ready HTML5 MVC.NET), because I want to be able to support clients from any device ... Windows PC, Apple, Windows Phone, iPhone, etc. If the call to User.Identity.Name correct, I would like to ask how it works on devices that are not Windows, like the iPhone? If my assumption is true that this will only work for Windows computers with a domain, then how can I achieve this? do i need to possibly use caching? If I could possibly grab the username and IP address, which will be used as the cache key on the authentication page?

My high-level question is ... How do I get the current username userName regardless of device / platform? I know this question is probably poorly written and can be difficult to understand ... I apologize for that. I am new to website development and try to keep my feet wet and want to start with the latest technology.

+6
source share
2 answers

The call is correct. User.Identity.Name populated with any authentication provider used β€” Windows authentication, form authentication, any user authentication provider, or something else. It is not tied to a specific user of "type". The authentication provider is responsible for ensuring that the Identity object matches the current user on every request. Usually this part is taken for the use of a combination of cookies and a database.

The MVC template (although I did not consider the template with MVC 2) uses the ASP.NET Membership class, which in turn uses the membership provider - for example, SqlMembershipProvider or ActiveDirectoryMembershipProvider - the former stores the credentials of your users (username and password, etc. .) in the SQL Server database, the latter uses Active Directory (that is, primarily to log on to Windows). SqlMembershipProvider by default, and MVC is configured to use the local SQLExpress database file as its user's repository.

The authentication provider implemented in the template project uses FormsAuthentication , which performs the login procedure using a simple HTML form (the one in the LogOn view) and allows the user to log in using an encrypted cookie. It works on any platform.

The settings for both FormsAuthentication and SqlMembershipProvider can be found in the web.config file (in the root directory of the site). There you can find connection strings for the SQLExpress database (and, for example, change them to use the "real" SQL Server, if necessary), a timeout for logins, etc.

(Note that you can easily do most of this configuration in the GUI via the "ASP.NET Settings" button on the Solution Explorer toolbar in Visual Studio - it also provides an easy way to configure first users).

In short, everything is ready to go - and does not block non-Windows users.

+6
source

As you said, User.Identity.Name really correct. to return the name of registered users. But the membership section, as you said, only provides Windows accounts. You can use similar windows accounts that aren’t taken into account, work in each scenario, and still check for windows, if any. If you call it without membership and follow the default MVC3 pattern, it should work fine.

 String Username = User.Identity.Name; 

When you log in using the MVC3 template, it creates authcookie . See Account Controller Code. Here two parameters are passed to it. Username and for saving (when the browser is closed - the login is still cached).

The username is a string field called User.Identity.Name and infact, everything can be put into it and has nothing to do with Windows login.

You can test the login using your preferred method, and if so, set the cookie using the authcookie method. (its encripted). And set the username to what you want. And if your user check failed, do not create it and do not return to the page.

See sample code. This is all from memory since I do not have the code for me for reference. But all this in the account controller, the login action.

When a cookie is set, the login state is cached for the session. You will need to make sure that the user is logged in when visiting the web page. Otherwise, loggin in would be pointless. This is a simple controller / action attribute.

Note. Do not do this with the account / login controller as you will not be able to visit the login page because you are not logged in.

 [Authorize] public ActionResult DoSomething() { // ... } 

Hope I helped.

+4
source

Source: https://habr.com/ru/post/908278/


All Articles