呵呵,真的,看了几天Profile 相关的东西了,可是还是不明白Profile 怎么样来进行验证,是不是得结合其它的方法来一起进行,这里我贴了一个实例出来(Profile 与Form 验证)但是,这个实例还是有些问题的,不能正确输出我想要输出的信息,,不过,我也搞不定它,不知道哪里错了,还希望朋友们帮帮忙,在这里先谢谢你们了,当然,希望各位能发表下自己对Profile 的认识,最好能有个更好的应用实例贴出来!
web.config
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
<authorization>
26
<deny users="?"/>
27
</authorization>
28
</system.web>
文件类:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
namespace Use.Profile
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
15
/// AddSum 的摘要说明
16
/// </summary>
17
[Serializable ]
18
public class AddSum
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
public AddSum()
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this._firstName; }
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this._firstName = value; }
33
}
34
public string LastName
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this._lastName; }
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this._lastName = value; }
38
}
39
public int MyMoney
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this._myMoney; }
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this._myMoney = value; }
43
}
44
public AddSum(string firstname, string lastname)
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
this._lastName = lastname;
47
this._firstName = firstname;
48
}
49
public int MoreMoney()
50![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
}
登录验证:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
protected void btnLogin_Click(object sender, EventArgs e)
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
Response.Write("I's a null object");
23
}
24
else
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
34
Response.Write(" something is wrong!");
35
}
36
}
37
[/code]
38
对Profile进行操作:
39
[code=C#]
40
protected void ForProfile(object sender, EventArgs e)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
42
if (Profile.myUser == null)
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44
Response.Write("I',m a anonymous use:<br>");
45
}
46
else
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
56
FormsAuthentication.SignOut();
57
}
谢谢!