asp.net2.0中membership用法總結

1.登陸驗證代碼

None.gifif (Membership.ValidateUser(userName.Text, password.Text))
ExpandedBlockStart.gif
dot.gif{
InBlock.gif
if (Request.QueryString["ReturnUrl"] != null)
ExpandedSubBlockStart.gif  
dot.gif{
InBlock.gif     FormsAuthentication.RedirectFromLoginPage(userName.Text,
false);
ExpandedSubBlockEnd.gif   }

InBlock.gif  
else
ExpandedSubBlockStart.gif  
dot.gif{
InBlock.gif     FormsAuthentication.SetAuthCookie(userName.Text,
false);
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif
else
ExpandedBlockStart.gif
dot.gif{
InBlock.gif Response.Write(
"Invalid UserID and Password");
ExpandedBlockEnd.gif}

None.gif

2.註冊代碼(通過profile擴展)

None.gif   MembershipCreateStatus ms;
None.gif   MembershipUser newuser
= Membership.CreateUser(TextBox1.Text, TextBox2.Text, TextBox4.Text, TextBox5.Text,TextBox6.Text,true, out ms);
None.gif
if (ms == MembershipCreateStatus.Success)
ExpandedBlockStart.gif
dot.gif{
InBlock.gifProfileCommon p
= (ProfileCommon)ProfileCommon.Create(newuser.UserName, true);
InBlock.gif Roles.AddUserToRole(TextBox1.Text,
"usermember");
InBlock.gifp.telphone
= TextBox7.Text.Trim();
InBlock.gif p.QQ
= TextBox8.Text.Trim();
InBlock.gif p.address
= TextBox9.Text.Trim();
InBlock.gif p.Save();
InBlock.gifRoles.AddUserToRole(TextBox1.Text,
"usergroup");      //添加新用戶到usergroup角色組
InBlock.gif
Server.Transfer("login.aspx");
ExpandedBlockEnd.gif }

None.gif
else
ExpandedBlockStart.gif
dot.gif{
InBlock.gif
string errorcode;
InBlock.gif
switch (ms)
ExpandedSubBlockStart.gif
dot.gif{
InBlock.gif
case MembershipCreateStatus.DuplicateUserName:
InBlock.gif errorcode
= "Username already exists.";
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

對應的web.config中profile節點的配置如下;

None.gif<profile enabled="true" defaultProvider="profileSqlMembershipProvider">
None.gif
<providers>
None.gif
<add name="profileSqlMembershipProvider"
None.giftype
="System.Web.Profile.SqlProfileProvider"
None.gifconnectionStringName
="webshopConnectionString"/>
None.gif
</providers>
None.gif
<properties>
None.gif
<add name="telphone" type="String"/>
None.gif
<add name="QQ" type="String"/>
None.gif
<add name="address" type="String"/>
None.gif
</properties>
None.gif
</profile>


4.驗證用戶名是否存在代碼

6.獲取當前用戶名
以上是在做網站過程中遇到的一些問題總結,希望對大家有用,同時也歡迎大家給我留言,討論asp.net技術

None.gifMembershipUserCollection col = Membership.FindUsersByName(this.TextBox1.Text);
None.gif
if (col.Count == 0)
None.gifLabel1.Text
= "恭喜您,可以註冊";
None.gif
else
None.gifLabel1.Text
= "此用戶已存在";

None.gifif (HttpContext.Current.User.Identity.IsAuthenticated == true)
ExpandedBlockStart.gif
dot.gif{
InBlock.gifLabel1.Text
= "您是註冊用戶,歡迎您的註冊";
ExpandedBlockEnd.gif}

None.gif
else
ExpandedBlockStart.gif
dot.gif{
InBlock.gifLabel1.Text
= "您是匿名用戶,請註冊";
ExpandedBlockEnd.gif}

5.判斷用戶是否登陸或者匿名代碼

None.gifMembershipUser u;
None.gifu
= Membership.GetUse(User.Identity.Name);
None.gifLabel1.Text
= u.UserName;
None.gif
None.gif
發佈了53 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章