Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Team

My controller does not seem to work, i have created following logic to create new users to my table from the database. The problem now is my UserId when its checked is null and how can i improve this better?

What I have tried:

  [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult CreateLogin(LoginCreateModel objSubmit)
        {
                if(objSubmit.btnSubmit == "Create")
            {
                ViewBag.Message = "Details saved successfully";
            }
            return View(objSubmit);
        }

       
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(Login login)
        {
            if (ModelState.IsValid)
            {
                bool success = WebSecurity.Login(login.username, login.password, false);
                var UserID = GetUserID_By_UserName(login.username);
                var LoginType = GetRoleBy_UserID(Convert.ToString(UserID));

                if (success == true)
                {
                    if (string.IsNullOrEmpty(Convert.ToString(LoginType)))
                    {
                        ModelState.AddModelError("Error", "Rights to User are not Provide Contact to Admin");
                        return View(login);
                    }
                    else
                    {
                        Session["Name"] = login.username;
                        Session["UserID"] = UserID;
                        Session["LoginType"] = LoginType;

                        if (Roles.IsUserInRole(login.username, "Admin"))
                        {
                            return RedirectToAction("AdminDashboard", "Dashboard");
                        }
                        else
                        {
                            return RedirectToAction("UserDashboard", "Dashboard");
                        }

                    }

                }
                else
                {
                    ModelState.AddModelError("Error", "Please enter valid Username and Password");
                    return View(login);
                }



            }
            else
            {
               ModelState.AddModelError("Error", "Please enter Username and Password");
                return View(login);
            }

        }

namespace eNtsaPortalWebsiteProject.Models
{
    public class LoginCreateModel
    {
        [Required]
        [StringLength(100,ErrorMessage = "The {0} must be least {2} characters long", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string password { get; set; }


        [Required]
        public string username { get; set; }

        public string btnSubmit { get; set; }
    }
}
Posted
Comments
ZurdoDev 27-Jan-20 8:15am    
You need to debug this. We can't run your code so I'm not sure what you want us to do.
gcogco10 27-Jan-20 9:00am    
UserID is empty, i have an insert statement on that table with data. Still i am unable to get value only null as a string on my stacktrace.
Richard Deeming 28-Jan-20 13:27pm    
So a method that we can't see, running against a database which we can't access, using input which we also can't see, is retuning null.

And you think we can debug that for you?

All I can suggest is that you move the GetUserID_By_UserName and GetRoleBy_UserID calls inside the if (success) branch, since they're only needed if the login succeeds.

Beyond that, you'll need to debug your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900