zoukankan      html  css  js  c++  java
  • 自定义继承自ProfileBase的Profile

    在配置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>


    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

  • 相关阅读:
    Linux系统管理之进程管理
    Linux交换空间swap讲解
    Linux物理存储结构以及磁盘划分
    Linux 文件系统挂载mount命令
    Linux文件系统df、du、fsck命令讲解
    Linux yum源配置以及yum命令讲解
    Linux rpm查询命令以及RPM包验证
    02Python常用模块_01math
    00_python常见用法笔记
    02_python闯关练习_02Max&Min
  • 原文地址:https://www.cnblogs.com/mxw09/p/1867441.html
Copyright © 2011-2022 走看看