FormsAuthentication Membership.GetUser() Null

发布于 2024-08-19 10:56:59 字数 545 浏览 3 评论 0原文

我正在使用表单身份验证并具有基本登录页面和默认页面。

当我在登录页面上并调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

回忆那么伤 2024-08-26 10:56:59

其他可以尝试的是以下代码:

 MembershipUser user = Membership.GetUser(username);
 GenericIdentity identity = new GenericIdentity(user.UserName);
 RolePrincipal principal = new RolePrincipal(identity);
 System.Threading.Thread.CurrentPrincipal = principal;
 HttpContext.Current.User = principal;

Something else to try is the following code:

 MembershipUser user = Membership.GetUser(username);
 GenericIdentity identity = new GenericIdentity(user.UserName);
 RolePrincipal principal = new RolePrincipal(identity);
 System.Threading.Thread.CurrentPrincipal = principal;
 HttpContext.Current.User = principal;
偏爱自由 2024-08-26 10:56:59

这可能是因为您在登录页面上允许匿名用户。因此,浏览器不会向该页面发送不必要的更多信息。

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.

指尖微凉心微凉 2024-08-26 10:56:59

我也遇到过同样的情况,这是在 MVC 4 .NET 4.5 上对我有用的方法。

Membership.GetUser(HttpContext.Current.User.Identity.Name)

I was in this same situation, here is what worked for me on MVC 4 .NET 4.5.

Membership.GetUser(HttpContext.Current.User.Identity.Name)
本王不退位尔等都是臣 2024-08-26 10:56:59

我遇到了类似的问题,问题是我在网络配置中缺少身份验证方法=表单。

<system.web>
    <authentication mode="Forms"/>
    ....

不要忘记这一点(我正在将一个旧的遗留站点迁移到 aspnet)

I had a similar issue and the problem turned out to be that I was missing authentication method = form in the web config .

<system.web>
    <authentication mode="Forms"/>
    ....

Don't forget that one (I was migrating an old legacy site to aspnet)

邮友 2024-08-26 10:56:59

我遇到了这个问题,发现这是由于有多个会员提供商造成的,所以

Membership.GetUser()

您可以尝试

Membership.Providers["MyMembershipProvider"].GetUser()

或更具体地说

Membership.Providers["MyMembershipProvider"].GetUser(LoginCtrl.UserName, false)

I had this issue and found it was due to having multiple membership providers, so instead of

Membership.GetUser()

you can try

Membership.Providers["MyMembershipProvider"].GetUser()

or more specifically

Membership.Providers["MyMembershipProvider"].GetUser(LoginCtrl.UserName, false)
蓝色星空 2024-08-26 10:56:59

我遇到了类似的问题(这是网站上的所有页面),它是由 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文