1.登陆验证代码
2.注册代码(通过profile扩展)

if (Membership.ValidateUser(userName.Text, password.Text))
{
if (Request.QueryString["ReturnUrl"] != null)
  
{
     FormsAuthentication.RedirectFromLoginPage(userName.Text,
false);
   }

  
else
  
{
     FormsAuthentication.SetAuthCookie(userName.Text,
false);
   }

}

else
{
Response.Write(
"Invalid UserID and Password");
}

 

 

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

else
{
string errorcode;
switch (ms)
{
case MembershipCreateStatus.DuplicateUserName:
errorcode
= "Username already exists.";
break;
}

}

对应的web.config中profile节点的配置如下;

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


4.验证用户名是否存在代码
5.判断用户是否登陆或者匿名代码
6.获取当前用户名
以上是在做网站过程中遇到的一些问题总结,希望对大家有用,同时也欢迎大家给我留言,讨论asp.net技术

MembershipUserCollection col = Membership.FindUsersByName(this.TextBox1.Text);
if (col.Count == 0)
Label1.Text
= "恭喜您,可以注册";
else
Label1.Text
= "此用户已存在";

 

if (HttpContext.Current.User.Identity.IsAuthenticated == true)
{
Label1.Text
= "您是注册用户,欢迎您的注册";
}

else
{
Label1.Text
= "您是匿名用户,请注册";
}

 

MembershipUser u;
u
= Membership.GetUse(User.Identity.Name);
Label1.Text
= u.UserName;

posted on 2009-05-13 12:03  yxbsmx  阅读(292)  评论(0编辑  收藏  举报