Membership.GetUser()。ProviderUserKey始终返回null [英] Membership.GetUser().ProviderUserKey always returns null

查看:144
本文介绍了Membership.GetUser()。ProviderUserKey始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我创造了在Visual Studio C#项目,它可以自动创建的网页,如/Account/Login.aspx。

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

我再接着安装一个例子 ASPNET _ * 表到我的SQL Server数据库,我已经能够使用< ASP :CreateUserWizardStep> 控件来创建一个用户

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.

我这才得以登录为这个用户,并在用户名登录时调用出现&LT; ASP:LoginName将&GT;

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

然而,当我打电话在我的C#code以下,在一个按钮单击事件处理程序,我总是得到一个空引用异常:

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();

应该不是这个返回用户名从我aspnet_Users表格内?

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

如果&LT; ASP:LoginName将&GT; 是显示用户名价值,我不应该总是能够调用 Membership.GetUser()。 ProviderUserKey

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

推荐答案

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

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.

我喜欢用这些夫妇的方法(第二个呼叫的第一个,但你也可以直接拨打第一个。我建议在调用第二个),它执行各种检查,并返回一个用户ID或空,如果有该用户没有通过验证或者身份不明的:

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;
    }

这篇关于Membership.GetUser()。ProviderUserKey始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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