简体   繁体   English

获取正确的User.Identity

[英]Getting the right User.Identity

I'm having a little bit of trouble with MVC HttpContext.User.Identity I'm getting the system.security.principal.windowsIdentity and it's giving me the name of my windows user... I don't want that, I want to get the (at first) null User.Identity so I can use FormsAuthentication. 我在使用MVC HttpContext.User.Identity时遇到了一些麻烦,正在获取system.security.principal.windowsIdentity,它为我提供了Windows用户的名称...我不需要,我想要以获取(首先)为空的User.Identity,因此我可以使用FormsAuthentication。 How can I prevent the mvc to use this system.security.principal.windowsIdentity to show up the right User.Identity? 如何防止MVC使用此system.security.principal.windowsIdentity显示正确的User.Identity? Any help would be appreciated 任何帮助,将不胜感激

    public ActionResult Index()
    {
        var s = HttpContext.User.Identity.Name; //
        return View();
    }

In the <system.web> section of your web.config file, change to forms authentication. 在web.config文件的<system.web>部分中,更改为表单身份验证。

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

This is because you have windows authentication enabled. 这是因为您启用了Windows身份验证。 You probably are in the same domain as your webserver so it will authenticate you automaticly. 您可能与网络服务器位于同一域中,因此它将自动对您进行身份验证。 Turn it off or be explicit about authentication in your configuration. 将其关闭或在配置中明确说明身份验证。

check the web.config to ensure <authentication mode="Forms" /> if you didn't set this explicitly, then you probably selected the mvc intranet template when you first created the project. 检查web.config以确保如果未显式设置<authentication mode="Forms" /> ,则可能在首次创建项目时选择了mvc intranet模板。

once the config file is updated you will also need to create the login/off actions and views. 配置文件更新后,您还需要创建登录/注销操作和视图。 you may want to create another project using the internet template and pull out the authentication components. 您可能要使用Internet模板创建另一个项目,然后取出身份验证组件。

intranet template defaults to windows authentication while internet template defaults to form. Intranet模板默认为Windows身份验证,而Internet模板默认为Form。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM