现在的位置: 首页 > 综合 > 正文

自定义继承自ProfileBase的Profile

2013年10月14日 ⁄ 综合 ⁄ 共 3050字 ⁄ 字号 评论关闭

http://www.cnblogs.com/mxw09/archive/2010/11/02/1867441.html

在配置web.config之前调用在VS2008命令行工具中运行 aspnet_regsql ,将对应的表安装到指定数据库。
在web.config里对Profile进行如下配置

 

<connectionStrings>
    <add name="myConn" connectionString="server=.;integrated security=true;database=AB;"/>
  </connectionStrings>
<profile inherits="MyClasses.MyPro" defaultProvider="MyProfileProvider" enabled="true" automaticSaveEnabled="true" >

      <providers>
        <add name="MyProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="myConn" />
      </providers>  
    </profile>

也可以这样配置(这样类方法上就不需要写太多的属性限制了):

    <anonymousIdentification enabled="true"/>
    <profile enabled="true" defaultProvider="AspNetSqlProfileProvider">
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider"  connectionStringName="NorthwindConnectionString" applicationName="/"/>
      </providers>
      <properties>
        <add name="ShoppingCart" type="ShopCardProfile.ProfileShoppingCart" allowAnonymous="true" serializeAs="Binary"/>
      </properties>
    </profile>

MyPro.cs如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Profile;

namespace MyClasses
{
    public class MyPro : ProfileBase
    {
        [System.Web.Profile.SettingsAllowAnonymous(true)]
        [System.Web.Profile.ProfileProvider("MyProfileProvider")]
   [System.Configuration.SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Binary)]  //使用二进制序列化,可选。
        public string MyPhone
        {
            //第一种方法
            get { return base["MyPhone"].ToString(); }
            set { base["MyPhone"] = value; }
            //第二种方法
            /*
                 get {
                return ((string)(this.GetPropertyValue("MyPhone")));
            }
            set
            {
                this.SetPropertyValue("MyPhone", value);
            }
            */
        }
    }
}

 

Default.aspx如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestWeb._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script runat="server" type="text/C#">
        protected void Button1_Click(object sender, EventArgs e)
        {
            this.Profile.MyPhone = txtPhone.Text;
            this.Profile.Save();
            
        }
    </script>
</head>
<body>
<%=  this.Profile.MyPhone%>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
    </div>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </form>
</body>
</html>

单击按钮,数据就会被保存到数据库。

注:1.匿名用户的cookie名称为 .ASPXANONYMOUS
2.登录用户的cookie名称为 .ASPXAUTH
3.若用户登陆以后,HttpContext.Profile取的就是登录用户的Profile而不是匿名用户的Profile
4.以上三点都是基于Forms验证得出的结论,如果使用windows验证,则不存在匿名不匿名的问题:(1)Profile.UserName总是为windows登录的用户名;(2)不会有 .ASPXANONYMOUS这个Cookie;(3)即使在后台代码中使用FormsAuthentication.SetAuthCookie("用户名",
false)
登录以后Profile.UserName还是为windows登录的用户名;(4)无论是否使用FormsAuthentication.SetAuthCookie("用户名", false)登录,在数据库中存储的Profile表中的UserID都是根据windows用户名生产的一个ID

抱歉!评论已关闭.