MemberShip创建一个新角色

      1.在MemberShip的aspnet_Roles表里面添加一个新角色的代码:

           System.Web.Security.Roles.CreateRole( " Provider " );
            
if  (System.Web.Security.Roles.RoleExists( " Provider " ))
            
{
                Response.Write(
"<script>alert('创建成功!')</script>");
            }


            说明:在aspnet_Roles添加一个"Provider"角色。
      2.在MemberShip判断该账号是否存在的代码:
            //查找该账号是否存在

           MembershipUserCollection a  =  Membership.FindUsersByEmail( " 账号(如邮箱) " );

            
if  (a.Count  ==   1 )
            
{
                
foreach (MembershipUser ms in a)
                
{
                    
string userName = ms.UserName.ToString();
                     
//如果存在该账号,给该账号分配"Provider"权限。
                    System.Web.Security.Roles.AddUserToRole(userName,"Provider");
                     
if (System.Web.Security.Roles.GetRolesForUser(userName!= null)
                    
{
                        Response.Write(
"<script>alert('配置成功!')</script>");
                    }

                     
else
                     
{
                         Response.Write(
"<script>alert('配置失败!')</script>");

                      }

                        
                }


            }

            
else
            
{
                Response.Write(
"<script>alert('账号不存在!')</script>");
            }


           

你可能感兴趣的:(IP)