locked
membership plugin RRS feed

  • Question

  • User1519602350 posted

    Code:

    if (IsPost && Validation.IsValid()) {
            var username = Request["UserName"];
            var password = Request["Password"];
            WebSecurity.CreateUserAndAccount(username, password);
            Roles.AddUsersToRoles(new[] { username }, new[] { "Author" });
            Response.Redirect("~/Admin");
        }

    Requirement:

    I am using webpages membership plugin. Above is my code,  i would like to include duplicate username check model state error. Please help me on this,

    Thanks

    Tuesday, August 13, 2013 4:54 AM

Answers

  • User540310021 posted
                var username = Request["UserName"];
                var password = Request["Password"];
    
                var db = Database.Open("yourdatabase");
               
                // Check if user already exists
                var user = db.QuerySingle("SELECT UserName FROM Yourtable WHERE LOWER(UserName) = LOWER(@0)", username);  
                
                if(user == null){
    
                    WebSecurity.CreateUserAndAccount(username, password);
                    Roles.AddUsersToRoles(new[] { username }, new[] { "Author" });
                    Response.Redirect("~/Admin");
    
                }  
                else{
                    ModelState.AddFormError("username already in use");
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 13, 2013 5:35 AM

All replies

  • Tuesday, August 13, 2013 4:59 AM
  • User540310021 posted
                var username = Request["UserName"];
                var password = Request["Password"];
    
                var db = Database.Open("yourdatabase");
               
                // Check if user already exists
                var user = db.QuerySingle("SELECT UserName FROM Yourtable WHERE LOWER(UserName) = LOWER(@0)", username);  
                
                if(user == null){
    
                    WebSecurity.CreateUserAndAccount(username, password);
                    Roles.AddUsersToRoles(new[] { username }, new[] { "Author" });
                    Response.Redirect("~/Admin");
    
                }  
                else{
                    ModelState.AddFormError("username already in use");
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 13, 2013 5:35 AM