web.config
1
<system.web>2
<anonymousIdentification enabled="true"/>3
<profile enabled="true" automaticSaveEnabled="true">4
<properties >5
<clear/>6
<add name="myUser" type="Use.Profile.AddSum" allowAnonymous="true" serializeAs="Binary"/>7
</properties>8
</profile>9
<compilation debug="true" />10
<roleManager enabled="true" cacheRolesInCookie="true">11
</roleManager>12
<authentication mode="Forms">13
<forms loginUrl="Logon.aspx" name=".hdd">14
<credentials passwordFormat="Clear">15
<user name="hdd" password="hdd"/>16
<user name ="test" password="test"/>17
<user name ="admin" password="admin"/>18
<user name="Kim"19
password="07B7F3EE06F278DB966BE960E7CBBD103DF30CA6"/>20
</credentials>21
22
</forms>23
</authentication>24

25
<authorization>26
<deny users="?"/>27
</authorization> 28
</system.web>文件类:
1

2
using System;3
using System.Data;4
using System.Configuration;5
using System.Web;6
using System.Web.Security;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Web.UI.WebControls.WebParts;10
using System.Web.UI.HtmlControls;11

12
namespace Use.Profile13


{14

/**//// <summary>15
/// AddSum 的摘要说明16
/// </summary>17
[Serializable ]18
public class AddSum19

{20
public AddSum()21

{22
//23
// TODO: 在此处添加构造函数逻辑24
//25
}26
private string _firstName;//="first";27
private string _lastName;//="second";28
private int _myMoney=2000;29
public string FirstName30

{31

get
{ return this._firstName; }32

set
{ this._firstName = value; }33
}34
public string LastName35

{36

get
{ return this._lastName; }37

set
{ this._lastName = value; }38
}39
public int MyMoney40

{41

get
{ return this._myMoney; }42

set
{ this._myMoney = value; }43
}44
public AddSum(string firstname, string lastname)45

{46
this._lastName = lastname;47
this._firstName = firstname;48
}49
public int MoreMoney()50

{51
this._myMoney++;52
return this._myMoney;53
}54
//public string returnMyInfo()55
//{56
// return ("my name is " + this._firstName.ToString () + "." + this._lastName.ToString () + ", and I hava " + string.Format("{0:C}", this._myMoney) + " Million money");57
//}58
}59
}登录验证:
1

2
protected void btnLogin_Click(object sender, EventArgs e)3

{4

/**/////if((this.txtName.Text .ToString ()=="hdd")&&(this.txtPsw.Text.ToString ()=="hdd"))5
////{6
//// FormsAuthentication.RedirectFromLoginPage(this.txtName.Text.ToString(), true);7
8
////}9
if ((bool)FormsAuthentication.Authenticate(this.txtName.Text.ToString(), this.txtPsw.Text.ToString()))//使用web.config文件中存放的用户 名和密码10

{11
//if (this.txtName.Text.ToString() == "hdd")12
//{13
// Roles.AddUsersToRole(new string[1] { "hdd" }, "NormalUser");14
//}15
//else16
//{17
// Roles.AddUsersToRole(new string[1] { "test" }, "AdminUser");18
//}19
AddSum add = new Use.Profile.AddSum(this.txtName.Text.ToString(), User.Identity.Name.ToString());20
if (add == null)21

{22
Response.Write("I's a null object");23
}24
else25

{26
Profile.myUser = add;27
Profile.myUser.FirstName = this.txtName.Text.ToString();28
Profile.myUser.LastName = "huang";29
FormsAuthentication.RedirectFromLoginPage(this.txtName.Text.ToString(), true);30
}31
}32
else33

{34
Response.Write(" something is wrong!");35
}36
}37
[/code]38
对Profile进行操作:39
[code=C#]40
protected void ForProfile(object sender, EventArgs e)41

{42
if (Profile.myUser == null)43

{ 44
Response.Write("I',m a anonymous use:<br>"); 45
}46
else47

{48
Profile.myUser.MoreMoney();49
Response.Write("I'm a normal user:<br>");50
Response.Write(User.Identity .Name .ToString ()+":"+ Profile.myUser.FirstName + "." + Profile.myUser.LastName + " have " + string.Format("{0:C}", Profile.myUser.MyMoney));51
// Response.Write(Profile.myUser.returnMyInfo());52
}53
}54
protected void logout(object sender, EventArgs e)55

{56
FormsAuthentication.SignOut();57
}谢谢!