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.
source share