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

发布于 2024-09-05 18:29:37 字数 1605 浏览 8 评论 0原文

所以我有一个 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; }
    }
}

此代码基于

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

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

谢谢,

So I have an asp.net Web Application (Not Web Site) that I am trying to support profiles for anonymous users. I have a form and I want anonymous users to be able to enter their name and email only once, and have that information automatically accessible on the next load for them.

In my Web.config I have anonymous ID setup like so:

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

I have my profile section setup like this:

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

Finally, due to my app being a Web App and not a Web Site, I am using the profiles via this custom object:

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 code is based off of this reference.

The issue is that that code does not support anonymous users, or if it does I don't know how. I can't use the GetUserProfile() method with no parameters because if the user is anonymous, Membership.GetUser() is null. I could pass in the anonymous ID token into the first GetUserProfile(string username) method but I cant' find any way to get the anonymous ID token for the current user.

Does anyone know how to get this information? Google doesn't seem to be returning useful results.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎言笑 2024-09-12 18:29:37

成功!

我改变了:

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

现在

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

它可以工作了!

Success!

I changed:

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

to

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

and now it works!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文