asp.net membership常见问题总结
1.登陆验证代码
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");
}
{
if (Request.QueryString["ReturnUrl"] != null)
{
FormsAuthentication.RedirectFromLoginPage(userName.Text, false);
}
else
{
FormsAuthentication.SetAuthCookie(userName.Text, false);
}
}
else
{
Response.Write("Invalid UserID and Password");
}
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)
{
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;
}
}
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>
<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 = "此用户已存在";
if (col.Count == 0)
Label1.Text = "恭喜您,可以注册";
else
Label1.Text = "此用户已存在";
5.判断用户是否登陆或者匿名代码
if (HttpContext.Current.User.Identity.IsAuthenticated == true)
{
Label1.Text = "您是注册用户,欢迎您的注册";
}
else
{
Label1.Text = "您是匿名用户,请注册";
}
{
Label1.Text = "您是注册用户,欢迎您的注册";
}
else
{
Label1.Text = "您是匿名用户,请注册";
}
6.获取当前用户名
MembershipUser u;
u = Membership.GetUse(User.Identity.Name);
Label1.Text = u.UserName;