如何为ASP.NET成员资格中的匿名用户获取aspnet_Users.UserId?

时间:2009-10-25 05:40:27

标签: asp.net-membership anonymous-users

我正在尝试从匿名用户那里获取aspnet成员资格UserId字段。

我在web.config中启用了匿名识别:

<anonymousIdentification enabled="true" />

我还创建了一个个人资料:

<profile>

  <providers>
    <clear />
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
         connectionStringName="ApplicationServices" 
         applicationName="/" />
  </providers>

  <properties>
    <add name="Email" type="System.String" allowAnonymous="true"/>
    <add name="SomethingElse" type="System.Int32" allowAnonymous="true"/>
  </properties>

</profile>

我在aspnetdb\aspnet_Users表中查看了已设置个人资料信息的匿名用户的数据。

Userid                                  PropertyNames   PropertyValuesString     
36139818-4245-45dd-99cb-2a721d43f9c5    Email:S:0:17:   me@example.com

我只需要找到如何获取'Userid'值。

无法使用:

 Membership.Provider.GetUser(Request.AnonymousID, false); 

Request.AnonymousID是一个不同的GUID,不等于36139818-4245-45dd-99cb-2a721d43f9c5。

有没有办法获得此Userid。我想将它与匿名用户的不完整活动相关联。使用aspnet_Users的主键比必须创建我自己的GUID(我可以做并存储在配置文件中)更可取。

这基本上是一个dupe of this question,但这个问题从未真正回答过。

2 个答案:

答案 0 :(得分:4)

您可以通过以下方式获取匿名用户的UserId:

string userId = Request.AnonymousID

答案 1 :(得分:2)

好的,MemberShip.GetUser()会返回MembershipUser类型的对象。

aspnetdb数据库的表中进行更多研究似乎虽然在aspnet_Users中为所有用户(甚至是匿名用户)创建了一行 - 但是在aspnet_Membership中不会创建一行,除非实际使用了useris认证/注册。

但是,匿名用户配置文件(aspnet_Users表)被赋予GUID而不是UserName

我仍然没有找到一种方法来访问匿名用户的UserId属性 - 但我已经意识到,无论如何用户名都是更有用的密钥。

UserId表中也存在aspnet_Users字段,因此如果您确实需要唯一的UserId字段,那么您可以通过“手动”SQL查找来获取它。