web.config中的profile

aspnet_regsql命令创建需要的表结构

 1  public class UserProfile:ProfileBase
 2     {
 3         [SettingsAllowAnonymous(true)]  //默认匿名用户不能访问
 4         public string MyTest 
 5         {
 6             get
 7             {
 8                 return (string)base["mytest"];
 9             }
10             set
11             {
12                 base["mytest"] = value;
13             }
14         }
15 
16     }
View Code
 1   <system.web>
 2         <compilation debug="true" targetFramework="4.0" />
 3         <anonymousIdentification enabled="true"/>
 4         <authentication mode="Forms">
 5             <forms loginUrl="a.html"></forms>
 6         </authentication>
 7         <profile defaultProvider="mySqlProfileProvider" inherits="WebApplication1.UserProfile">
 8             <providers>
 9                 <clear/>
10                 <add name="mySqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" applicationName="aa.com"  connectionStringName="aspnetdb"    description="mySqlProfileProvider"/>
11             </providers>
12             <properties>
13                 <add name="A" /> <!--默认是string类型,匿名用户能够访问-->
14                 <add name="B" type="System.Int32"/>
15                 <add name="C" allowAnonymous="false" />
16                 <group name="lists">
17                     <add name="a" type="int"/>
18                 </group>
19             </properties>
20         </profile>
21     </system.web>
22     <connectionStrings>
23         <add name="aspnetdb" connectionString="Data Source=.;Initial Catalog=aspnetdb;Persist Security Info=True;User ID=sa;Password=123" providerName="System.Data.SqlClient"/>
24     </connectionStrings>
View Code

在properties中添加的name会放到默认生成的对象ProfileCommon中,ProfileCommon默认继承ProfileBase,但是可以自定义Profile,此时ProfileCommon会继承自定义的Profile【如上文中的UserProfile】。Profile中的信息会持久化到指定的数据库中。

原文地址:https://www.cnblogs.com/goodlucklzq/p/4403030.html