入门登录的用户名在ASP.NET MVC3 Intranet应用程序 [英] Getting the logged in username in ASP.NET MVC3 intranet application

查看:71
本文介绍了入门登录的用户名在ASP.NET MVC3 Intranet应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个MVC 3 Intranet应用程序(Windows身份验证)。应用程序必须在显示用户一旦用户登录的个人资料页面。为了做到这一点的登录用户的用户名必须是在Global.asax.cs中以下路由的路由参数传入。

I am working on a MVC 3 intranet application ( windows authentication ). The application must display the profile page of the user once a user logs in. In order to do that the username of the logged in user must be passed in as a route parameter in the following route in Global.asax.cs.

routes.MapRoute(
    "Initial",
    "{controller}/{action}/{emailAlias}", // URL with parameters
    new { controller = "Home", action = "Home", userId = **<USERNAME>**}
);

在这里,为我用一些替代品。

Here, for the I've used some alternatives.


  1. 起初我用 Environment.Username 。效果很好的发展。但不发布后。因为那时 Environment.Username 产生哪些应用程序在运行应用程序池的名称。如所描述的此处

  1. At first I used Environment.Username. Which works well in development. But not after publishing. Because then Environment.Username yields the name of the applications pool which the app runs in. As described here.

Controller.User.Identity.Name 用于获取所需的用户名的控制器,在pre和发布后效果很好。但它不能在Global.asax.cs中的上下文中使用。

Controller.User.Identity.Name is used to get the desired username in controllers, works well in pre and post publishing. But it cannot be used in the context of Global.asax.cs.

System.Web.HttpContext.Current.User.Identity.Name 产生空引用。

System.Security.Principal.WindowsIdentity.GetCurrent()。名称工程样的相同 Environment.Username

我敢肯定,它很容易被现在弄清楚,我是小白。也许我错过了一些东西明显。如果有,请指出这一点还是请大家告诉我一个简单的解决方案由于提前。

I'm sure it's easy to figure out that I'm a noob by now. Maybe I've missed something obvious. If so please point that out or please tell me of a simple solution thanks in advance.

推荐答案

在Global.asax code被认为在应用程序启动(和关闭)来运行,并没有在任何会议或用户时间(故名全球)。为什么你需要路由的用户名?你应该只使用 User.Identity.Name 在控制器的code来标识用户,而不是在得到它作为一个参数依托的。

The global.asax code is supposed to be run at application startup (and shutdown) and you don't have any session or a user at that time (hence the name global). Why do you need the username in the route? You should just use User.Identity.Name in the controllers' code to identify the user instead of relying in getting it as a parameter.

这篇关于入门登录的用户名在ASP.NET MVC3 Intranet应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆