ASP.NET MVC HttpContext登录后获取IdentityName [英] ASP.NET MVC HttpContext Getting IdentityName after LogOn

查看:128
本文介绍了ASP.NET MVC HttpContext登录后获取IdentityName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HttpContext.Current.User.Identity.Name在登录后返回null.我正在使用IIS7.0 Framework 4.0.和vs2010.我有另一个项目,其targetFramework是3.5.效果很好.但是我的新项目的targetFramework是4.0.并在调用HttpContext.Current.User.Identity.Name时返回null

HttpContext.Current.User.Identity.Name returns null after LogOn. I am using IIS7.0 framework 4.0. and vs 2010. i have another project which targetFramework is 3.5. it works good. But targetFramework of my new project is 4.0. and when calling HttpContext.Current.User.Identity.Name it returns null

推荐答案

登录后,应先发出HTTP重定向,然后才能使用此属性.重定向后,您将可以在后续请求中使用它.这是通常的模式:

You should issue an HTTP redirect after logging in before being able to use this property. After the redirect you will be able to use it on subsequent requests. Here's the usual pattern:

public ActionResult LogOn()
{
    FormsAuthentication.SetAuthCookie("someuser", false);
    return RedirectToAction("foo");
}

[Authorize]
public ActionResult Foo()
{
    // use the logged in user here without problems
    string userName = User.Identity.Name;
    return View();
}

这篇关于ASP.NET MVC HttpContext登录后获取IdentityName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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