【问题标题】:How to get aspnet_Users.UserId for an anonymous user in ASP.NET membership?如何获取 ASP.NET 成员身份中匿名用户的 aspnet_Users.UserId?
【发布时间】:2010-12-09 20:35:28
【问题描述】:

我正在尝试从匿名用户那里获取 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,但这个问题从未真正得到回答。

【问题讨论】:

    标签: asp.net-membership anonymous-users


    【解决方案1】:

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

    string userId = Request.AnonymousID

    【讨论】:

      【解决方案2】:

      好的,所以MemberShip.GetUser() 返回一个MembershipUser 类型的对象。

      深入研究aspnetdb 数据库中的表,似乎尽管在aspnet_Users 中为所有用户(甚至是匿名用户)创建了一行 - 除非用户实际经过身份验证/注册,否则不会在 aspnet_Membership 中创建一行。

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

      我仍然没有找到访问匿名用户的UserId 属性的方法 - 但我意识到用户名确实是更有用的键。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-11
        • 2010-11-19
        • 1970-01-01
        相关资源
        最近更新 更多