Membership.Providers GetUser在使用用户名时返回null

时间:2013-02-04 04:59:23

标签: c# asp.net-mvc-3 asp.net-membership membership-provider

top函数返回成员资格用户,而第二个函数则不返回。我已经为顶级函数获得结果的用户传递了用户名,但第二个只返回null。是否有基于用户名获取用户的建议?

public static MembershipUser getCCUser(Guid UserID)
{
   return ((MembershipProvider)Membership.Providers["CC_MembershipProvider"]).GetUser(UserID, false);
}

public static MembershipUser getCCUser(string userName)
{
   return ((MembershipProvider)Membership.Providers["CC_MembershipProvider"]).GetUser(userName, false);
}

3 个答案:

答案 0 :(得分:4)

web.config中的 applicationNames 应与 aspnet_Applications 表中的应用程序名称相匹配

答案 1 :(得分:2)

我发现了导致问题的真正原因。最近我更改了应用程序名称,以免与成员资格中的其他应用程序冲突。我忘记在web.config文件的成员资格部分更新应用程序名称:

Web.config(为简洁起见):

<membership>
      <providers>
        <clear/>
        <add name="AA_MembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="AA_MEMBERSHIP" applicationName="/"/>
        <add name="CC_MembershipProvider" applicationName="/"/>
      </providers>
</membership>

应该读过:

<membership>
      <providers>
        <clear/>
        <add name="AA_MembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="AA_MEMBERSHIP" applicationName="/"/>
        <add name="CC_MembershipProvider" applicationName="CCPortal"/>
      </providers>
</membership>

有趣的是,我在使用基于Guid的用户ID时找到了用户,但在根据字符串用户名查找用户时,成员资格需要具有正确的应用程序范围。

答案 2 :(得分:0)

确定。所以这只是一种可能的方式,但基本上我只是使用Entity Framework进行查找。我仍然愿意接受建议。

public static MembershipProvider CCMembershipProvider
{
    get
    {
        return ((MembershipProvider)Membership.Providers["CC_MembershipProvider"]);
    }
}

public static MembershipUser getCCUser(Guid UserID)
{
    return CCMembershipProvider.GetUser(UserID, false);
}

public static MembershipUser getCCUser(string userName)
{
    //This function is BS.  For some reason userName always gets null.  Unfortunately UsersInRole only gives a string[]
    //Soo... Here is the jenky workaround...
    userName = userName.ToLower();

    CCPortal.MEMBERSHIPEntities context = new CC.MEMBERSHIPEntities();

    CCPortal.aspnet_Users user = context.aspnet_Users.SingleOrDefault(u => u.LoweredUserName == userName);

    return getCCUser(user.UserId);
    //This is what We should be using....
    //return CCMembershipProvider.GetUser(userName, false);
}