简体   繁体   English

ASP.NET身份-匿名配置文件

[英]ASP.NET Identity - Anonymous profiles

I want to use anonymous profiles to store and use profile data without authenticated. 我想使用匿名配置文件来存储和使用未经身份验证的配置文件数据。 How to enable anonymous profiles is described on page https://msdn.microsoft.com/en-us/library/ewfkf772(v=vs.100).aspx https://msdn.microsoft.com/zh-cn/library/ewfkf772(v=vs.100).aspx上介绍了如何启用匿名配置文件

Profiles can also work with anonymous users. 个人资料也可以与匿名用户一起使用。 Support for anonymous profiles is not enabled by default, so you must explicitly enable it. 默认情况下,不启用对匿名配置文件的支持,因此您必须显式启用它。 In addition, when you define profile properties in the Web.config file, you must explicitly make them available individually for anonymous users. 另外,当您在Web.config文件中定义配置文件属性时,必须显式使它们分别可用于匿名用户。 Profile properties do not support anonymous access by default because profiles may be designed to work with authenticated users, and many properties are likely to pertain to personal information that is not available for anonymous users. 配置文件属性默认情况下不支持匿名访问,因为配置文件可能设计为与经过身份验证的用户一起使用,并且许多属性很可能与匿名用户不可用的个人信息有关。

But I'm not sure, that I can use profile data for anonymous users. 但是我不确定是否可以将配置文件数据用于匿名用户。 It says, that by default I can't use profile data. 它说,默认情况下,我不能使用配置文件数据。 But not by default, have I ability to use profile data for anonymous users and if "yes" then what should I do? 但是默认情况下不是这样,我是否可以为匿名用户使用配置文件数据,如果“是”,我该怎么办? I confused... 我很困惑

Answer 回答

Anonymous profiles are possible. 匿名配置文件是可能的。 They let us associate profile data (eg a postal code) with an anonymous user. 他们让我们将个人资料数据(例如邮政编码)与匿名用户相关联。 Of course, the profile data is only relevant while ASP.NET can identify the anonymous user via a cookie (or a query string parameter) like this: 当然,概要文件数据仅在ASP.NET可以通过如下cookie(或查询字符串参数)识别匿名用户时才相关:

匿名Cookie

But I'm not sure, that I can use profile data for anonymous users. 但是我不确定是否可以将配置文件数据用于匿名用户。

We can. 我们可以。 I've just tested this on my local machine with ASP.NET MVC. 我刚刚在本地计算机上使用ASP.NET MVC对此进行了测试。

...and if "yes" then what should I do? ...如果是,那我该怎么办?

First, add the following to the system.web section of the web.config file. 首先,将以下内容添加到web.config文件的system.web部分。 It tells ASP.NET to track anonymous users, add a single profile property, and configure the provider with our default DB connection (assuming we have a connection string named DefaultConnection .) 它告诉ASP.NET跟踪匿名用户,添加单个配置文件属性,并使用默认的数据库连接配置提供程序(假设我们有一个名为DefaultConnection的连接字符串)。

<anonymousIdentification enabled="true" />   
<profile defaultProvider="SqlProvider" >
  <properties>
    <add name="PostalCode"
      type="System.String"
      allowAnonymous="true" />               
  </properties>
  <providers>
    <clear />
    <add name="SqlProvider"
         type="System.Web.Profile.SqlProfileProvider"
         connectionStringName="DefaultConnection" />
  </providers>      
</profile>  

Once we've done that, ASP.NET will allow us to get/set profile properties as follows: 完成此操作后,ASP.NET将允许我们按以下方式获取/设置配置文件属性:

// get
var postalCode = Profile.GetPropertyValue("PostalCode") as string;

// set
Profile.SetPropertyValue("PostalCode", "V8K 1B1");
Profile.Save();

Note: you must setup Profiles on your database for this to work (otherwise ASP.NET will complain that it cannot find stored procedures.) One way to setup Profiles is to run aspnet_regsql on your database like this. 注意:必须在数据库上设置配置文件才能起作用(否则ASP.NET将抱怨它找不到存储过程。)设置配置文件的一种方法是在数据库上运行aspnet_regsql ,如下所示。

aspnet_regsql -S "myDbServer" -A p -d "myDbName" -E

For more info about the aspnet_regsql flags, run aspnet_regsql -? 有关aspnet_regsql标志的更多信息,请运行aspnet_regsql -? for documentation. 用于文档。 The -A p sets up Profiles and the -E uses integrated security. -A p设置概要文件, -E使用集成的安全性。

See also: 也可以看看:

For other approaches and more details, here are some other links: 有关其他方法和更多详细信息,请参见以下其他链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM