1.登陆验证代码
if (Membership.ValidateUser(userName.Text, password.Text))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (Request.QueryString["ReturnUrl"] != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FormsAuthentication.RedirectFromLoginPage(userName.Text, false);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FormsAuthentication.SetAuthCookie(userName.Text, false);
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Response.Write("Invalid UserID and Password");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2.注册代码(通过profile扩展)
MembershipCreateStatus ms;
MembershipUser newuser = Membership.CreateUser(TextBox1.Text, TextBox2.Text, TextBox4.Text, TextBox5.Text,TextBox6.Text,true, out ms);
if (ms == MembershipCreateStatus.Success)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
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
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string errorcode;
switch (ms)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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.验证用户名是否存在代码
MembershipUserCollection col = Membership.FindUsersByName(this.TextBox1.Text);
if (col.Count == 0)
Label1.Text = "恭喜您,可以注册";
else
Label1.Text = "此用户已存在";
5.判断用户是否登陆或者匿名代码
if (HttpContext.Current.User.Identity.IsAuthenticated == true)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Label1.Text = "您是注册用户,欢迎您的注册";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Label1.Text = "您是匿名用户,请注册";
}
6.获取当前用户名
MembershipUser u;
u = Membership.GetUse(User.Identity.Name);
Label1.Text = u.UserName;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)