HttpContext.Current.User.Identity.Name 始终为 string.Empty
您好,我使用自定义 MembershipProvider。
我想在应用程序场景中知道当前用户名,但是当我尝试访问 HttpContext.Current.User.Identity.Name 时,它总是返回 string.Empty。
if (Membership.ValidateUser(tbUsername.Text, tbPassword.Text))
{
FormsAuthentication.SetAuthCookie(tbUsername.Text, true);
bool x = User.Identity.IsAuthenticated; //true
string y = User.Identity.Name; //""
FormsAuthentication.RedirectFromLoginPage(tbUsername.Text, cbRememberMe.Checked);
}
我错过了什么吗?
Hi I use a custom MembershipProvider.
I want to know the current username during an application scenario, but when I try accessing HttpContext.Current.User.Identity.Name it always returns string.Empty.
if (Membership.ValidateUser(tbUsername.Text, tbPassword.Text))
{
FormsAuthentication.SetAuthCookie(tbUsername.Text, true);
bool x = User.Identity.IsAuthenticated; //true
string y = User.Identity.Name; //""
FormsAuthentication.RedirectFromLoginPage(tbUsername.Text, cbRememberMe.Checked);
}
Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您遇到的问题是,此时您仅设置身份验证 cookie,在表单身份验证模块内创建的 IPrincipal 在有新请求之前不会发生 - 所以此时 HttpContext.User 处于奇怪的状态。 一旦发生重定向,因为这是来自浏览器的新请求,所以在到达您的页面并创建正确的用户对象之前,将读取 cookie。
Cookie 仅在请求完成后才会在浏览器上设置。
顺便说一句,RedirectFromLoginPage 无论如何都会创建一个表单身份验证 cookie,您不需要手动执行此操作
The problem you have is at this point you're only setting the authentication cookie, the IPrincipal that gets created inside the forms authentication module will not happen until there is a new request - so at that point the HttpContext.User is in a weird state. Once the redirect happens then, because it's a new request from the browser the cookie will get read before your page is reached and the correct user object created.
Cookies are only set on the browser after a request is completed.
As an aside RedirectFromLoginPage creates a forms auth cookie anyway, you don't need to do it manually
请尝试使用
System.Web.HttpContext.Current.Request.LogonUserIdentity.Name
而不是User.Identity.Name
。 这对我有用。Please try
System.Web.HttpContext.Current.Request.LogonUserIdentity.Name
instead ofUser.Identity.Name
. It worked for me.HttpContext.Current.User.Identity.Name
的值是通过调用RedirectFromLoginPage
设置的。 重定向到新页面后,您可以从 HttpContext.Current.User.Identity.Name 获取当前用户 ID。 我不确定为什么您需要在这种情况下通过 User 属性访问用户名,难道您不能只使用 tbUsername.Text 中包含的值吗?The value of
HttpContext.Current.User.Identity.Name
is set by the call toRedirectFromLoginPage
. You can get the current user id fromHttpContext.Current.User.Identity.Name
once you are redirected to a new page. I'm not sure why you would need to access the user name through the User property in this context, couldn't you just use the value contained in tbUsername.Text?在 VS Community 2015 版本中,如果您创建 Web 表单应用程序,它会自动在 web.config 节点中添加代码以删除 FormsAuthentication,请尝试删除以下部分
in VS Community 2015 version, if you create a web forms application, it automatically add codes in web.config node to remove FormsAuthentication, try remove below section
正如已经建议的
FormsAuthentication.RedirectFromLoginPage()
方法,自动设置身份验证 Cookie。然而,就我而言,我有嵌套的 Web 应用程序,我在
web.config 中清除了子应用程序中的
文件。 删除不需要的父 httpModules 使一切再次正常工作。
标记(以便它不会从其父应用程序继承 httpModules)在事情变得复杂之前最好先检查一下这个标签:)
As already suggested
FormsAuthentication.RedirectFromLoginPage()
method, sets the Authentication Cookie automatically.However in my case, i had nested web applications where i had cleared
<httpModules>
tag in child application (so that it does not inherit httpModules from its parent application) in theweb.config
file. Removing the unwanted parent httpModules made everything work again.its better to check this tag before complicating things :)
如果您正在从会员提供商处查找用户名,请尝试这样的操作......
If you're looking for the name of the user from the membership provider, try something like this ...
如果您使用 URL 重写或更改 URL,可能会导致返回空 null 值。您应该尝试将 URL 的路径从 .html 更改为 .aspx 或不扩展。 这是我的情况的问题。你试试。我希望这有用
If you're using URL rewrite or change your URL,it may be cause return Empty null value.You should try change path of your URL from .html to .aspx or none extendtion. this is issue for my case.You try.I hope this useful