Membership.GetUser().ProviderUserKey 始终返回 null

发布于 2024-11-30 22:09:15 字数 676 浏览 1 评论 0原文

我最近开始使用 ASP.NET 表单身份验证和成员身份。

我在 Visual Studio 中创建了一个 C# 项目,它会自动创建“/Account/Login.aspx”之类的页面。

然后,我按照将 aspnet_* 表安装到 SQL Server 数据库的示例进行操作,并且我已经能够使用 控件来创建用户。

然后,我就能够以该用户身份登录,并且在调用 时会显示登录的用户名。

但是,当我在按钮单击事件中的 C# 代码中调用以下内容时处理程序,我总是收到空引用异常:

string UserID = Membership.GetUser().ProviderUserKey.ToString();

这不应该从我的 aspnet_users 表返回 UserID 吗?

如果 显示 UserName 值,我不应该总是能够调用 Membership.GetUser().ProviderUserKey

I've recently started using ASP.NET Forms Authentication and Membership.

I created a C# project in Visual Studio, which automatically created pages like "/Account/Login.aspx".

I then followed an example for installing aspnet_* tables to my SQL Server database, and I've been able to use the <asp:CreateUserWizardStep> control to create a user.

I've then been able to login as this user, and the logged in username appears when calling <asp:LoginName>

However, when I call the following in my C# code, in a Button Click Event Handler, I always get a Null Reference Exception:

string UserID = Membership.GetUser().ProviderUserKey.ToString();

Shouldn't this return the UserID from my aspnet_users table?

If <asp:LoginName> is showing a UserName value, shouldn't I always be able to call Membership.GetUser().ProviderUserKey

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

瞳孔里扚悲伤 2024-12-07 22:09:15

首先检查您是否拥有有效的经过身份验证的用户 ID。从你的问题来看,你好像确实有。但一系列的检查始终是一个好的做法。

我喜欢使用这两个方法(第二个方法调用第一个方法,但您也可以直接调用第一个方法。我建议调用第二个方法),它们执行各种检查并返回用户 ID,如果存在用户,则返回 null未验证或未识别:

    public static MembershipUser GetCurrentUser()
    {
        HttpContext httpContext = HttpContext.Current;
        if (httpContext != null && httpContext.User != null && httpContext.User.Identity.IsAuthenticated)
        {
            return Membership.GetUser();
        }

        return null;
    }

    /// <summary>
    /// Safe check of authenticity. Better than Request.IsAuthenticated in that if there's a used-to-be-valid cookie which does not correspond to the current database, it will fail safe
    /// </summary>
    /// <returns></returns>
    public static bool IsUserAuthenticated()
    {
        if (HttpContext.Current == null)
            return false;

        var request = HttpContext.Current.Request;

        if (!request.IsAuthenticated)
            return false;

        var membershipUser = GetCurrentUser();

        if (membershipUser != null)
            return true;

        return false;
    }

First check whether you have a valid authenticated user id. From your question, it sounds like you do have. But a series of checks is always a good practice.

I like to use these couple of methods (the second one calls the first, but you can also call the first one directly. I recommend calling the second one) which perform various checks and return a User ID or null if there is the user is not authenticated or unidentified:

    public static MembershipUser GetCurrentUser()
    {
        HttpContext httpContext = HttpContext.Current;
        if (httpContext != null && httpContext.User != null && httpContext.User.Identity.IsAuthenticated)
        {
            return Membership.GetUser();
        }

        return null;
    }

    /// <summary>
    /// Safe check of authenticity. Better than Request.IsAuthenticated in that if there's a used-to-be-valid cookie which does not correspond to the current database, it will fail safe
    /// </summary>
    /// <returns></returns>
    public static bool IsUserAuthenticated()
    {
        if (HttpContext.Current == null)
            return false;

        var request = HttpContext.Current.Request;

        if (!request.IsAuthenticated)
            return false;

        var membershipUser = GetCurrentUser();

        if (membershipUser != null)
            return true;

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