FormsAuthentication Membership.GetUser() Null
我正在使用表单身份验证并具有基本登录页面和默认页面。
当我在登录页面上并调用 SignOn 时,效果非常好。但是,当我仍在登录页面时,Membership.GetUser()
返回 null。当我重定向到默认页面时,Membership.GetUser()
返回我的用户信息。
有什么方法可以让我在登录页面上时返回我的用户。我在谷歌上读到其他人也有类似的问题,只有在重定向后它才会起作用。
如果您需要更多信息,请告诉我。
这是我用来验证信息是否已设置的简单代码片段:
bool authenticated = User.Identity.IsAuthenticated;
string username = User.Identity.Name;
MembershipUser user = Membership.GetUser();
我将此代码放在登录页面和代码后面的默认页面上,只有默认页面具有值并显示它已通过身份验证身份验证过程执行后。
I am using Forms Authentication and have the basic logon page and default page.
When I am on the logon page, and call the SignOn this works just great. However, when I am still on the Logon page the Membership.GetUser()
returns null. When I redirect to my default page, the Membership.GetUser()
returns my user information.
Is there any way I get get this method to return my user while still on the logon page. I have read all over google that other have similar issues where it will only work once you redirect.
Let me know if you need further information.
Here is a simple code snippet of what I am using to verify that the information is being set:
bool authenticated = User.Identity.IsAuthenticated;
string username = User.Identity.Name;
MembershipUser user = Membership.GetUser();
I put this code on both the logon page and the default page in the code behind and only the default page has values and shows that it is authenticated after the authentication process executes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
其他可以尝试的是以下代码:
Something else to try is the following code:
这可能是因为您在登录页面上允许匿名用户。因此,浏览器不会向该页面发送不必要的更多信息。
This might be because you allow anonymous users on your login page. Therefore the browser doesn't bother sending any more information to this page than is necessary.
我也遇到过同样的情况,这是在 MVC 4 .NET 4.5 上对我有用的方法。
I was in this same situation, here is what worked for me on MVC 4 .NET 4.5.
我遇到了类似的问题,问题是我在网络配置中缺少身份验证方法=表单。
不要忘记这一点(我正在将一个旧的遗留站点迁移到 aspnet)
I had a similar issue and the problem turned out to be that I was missing authentication method = form in the web config .
Don't forget that one (I was migrating an old legacy site to aspnet)
我遇到了这个问题,发现这是由于有多个会员提供商造成的,所以
您可以尝试
或更具体地说
I had this issue and found it was due to having multiple membership providers, so instead of
you can try
or more specifically
我遇到了类似的问题(这是网站上的所有页面),它是由 AspNetSqlMembershipProvider 连接字符串中的错误引起的,该错误在不同的环境中应该有所不同,但事实并非如此,因此它可以在本地工作,但在部署到服务器。
I had a similar problem (it was all pages on the site) and it was caused by an error in the AspNetSqlMembershipProvider connection string, which SHOULD have been different in different environments but wasn't, so it worked locally but not when deployed to the server.