web.config
Code
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<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>
文件类:
Code
1
2using System;
3using System.Data;
4using System.Configuration;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12namespace Use.Profile
13{
14 /**//// <summary>
15 /// AddSum 的摘要说明
16 /// </summary>
17 [Serializable ]
18 public class AddSum
19 {
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 FirstName
30 {
31 get { return this._firstName; }
32 set { this._firstName = value; }
33 }
34 public string LastName
35 {
36 get { return this._lastName; }
37 set { this._lastName = value; }
38 }
39 public int MyMoney
40 {
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
2using System;
3using System.Data;
4using System.Configuration;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12namespace Use.Profile
13{
14 /**//// <summary>
15 /// AddSum 的摘要说明
16 /// </summary>
17 [Serializable ]
18 public class AddSum
19 {
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 FirstName
30 {
31 get { return this._firstName; }
32 set { this._firstName = value; }
33 }
34 public string LastName
35 {
36 get { return this._lastName; }
37 set { this._lastName = value; }
38 }
39 public int MyMoney
40 {
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}
登录验证:
Code
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 //else
16 //{
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 else
25 {
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 else
33 {
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 else
47 {
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 }
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 //else
16 //{
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 else
25 {
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 else
33 {
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 else
47 {
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 }
谢谢!