如何获得配置文件支持的asp.net匿名身份验证令牌?

时间:2010-06-14 14:05:06

标签: asp.net forms-authentication asp.net-profiles anonymous-users asp.net-authentication

所以我有一个asp.net Web应用程序(非网站)我试图支持匿名用户的配置文件。我有一个表单,我希望匿名用户能够只输入一次他们的姓名和电子邮件,并在下次加载时自动访问这些信息。

在我的Web.config中,我有匿名ID设置,如下所示:

<anonymousIdentification enabled="true" cookieless="AutoDetect" />

我的个人资料部分设置如下:

<profile defaultProvider="SqlProvider" enabled="true" inherits="QA_Web_Tools.UserProfile">
  <providers>
    <clear />
    <add connectionStringName="QAToolsConnectionString" name="SqlProvider"
         type="System.Web.Profile.SqlProfileProvider" />
  </providers>
</profile>

最后,由于我的应用程序是Web应用程序而不是网站,我通过此自定义对象使用配置文件:

public class UserProfile : ProfileBase
{
    public static UserProfile GetUserProfile(string username)
    {
        return Create(username) as UserProfile;
    }

    public static UserProfile GetUserProfile()
    {
        return Create(Membership.GetUser().UserName) as UserProfile;
    }

    [SettingsAllowAnonymous(true)]
    public string FullName
    {
        get { return base["FullName"] as string; }
        set { base["FullName"] = value; }
    }

    [SettingsAllowAnonymous(true)]
    public string BuildEmail
    {
        get { return base["BuildEmail"] as string; }
        set { base["BuildEvmail"] = value; }
    }
}

此代码基于this reference

问题是该代码不支持匿名用户,或者如果它不支持,我不知道如何。我不能使用没有参数的GetUserProfile()方法,因为如果用户是匿名的,Membership.GetUser()为空。我可以将匿名ID令牌传递给第一个GetUserProfile(string username)方法,但我找不到任何方法来获取当前用户的匿名ID令牌。

有谁知道如何获取此信息?谷歌似乎没有返回有用的结果。

谢谢,

1 个答案:

答案 0 :(得分:2)

成功!

我改变了:

public static UserProfile GetUserProfile()
{
    return Create(Membership.GetUser().UserName) as UserProfile;
}

    public static UserProfile GetUserProfile()
    {
        return HttpContext.Current.Profile as UserProfile;
    }

现在它有效!

相关问题