0 Replies - 327 Views - Last Post: 17 March 2019 - 06:05 PM

#1 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 211
  • Joined: 01-July 12

ASP.Net Core MVC Login user

Posted 17 March 2019 - 06:05 PM

Hello everyone I have a ASP.Net Core MVC project I am working on and I just simply would like my user to login and once logged in be brought to the userhome page. I have made my model the controller and the view. in the View I am not sure 100% on how to pass the full user into my controller because I have a [Bind] User in my method. To pass in a individual property I would use Model.Id I'm assuming I need Model for the full object? I am completely new with ASP.Net MVC any more questions please ask I can give more info or any comments I'm interested in hearing them. Thank you
UserDataAccess login method with username or email not 100% if this is the correct way
public string ValidateLogin(User user)
        {
            using(SqlConnection conn = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("spUserLogin", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@LoginID", user.Username);
                cmd.Parameters.AddWithValue("@LoginEmail", user.Email);
                cmd.Parameters.AddWithValue("@LoginPassword", user.Password);

                conn.Open();
                string result = cmd.ExecuteScalar().ToString();
                conn.Close();

                return result;
            }
LoginController 
[HttpGet]
        public IActionResult UserLogin()
        {

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> UserLogin([Bind] User user)
        {
            //ModelState.Remove("Name");// ??

            if(ModelState.IsValid)
            {
                string loginStatus = objUser.ValidateLogin(user);

                if(loginStatus == "Success")
                {
                    var claims = new List<Claim>
                    {
                        new Claim(ClaimTypes.PrimarySid, user.Id.ToString(), ClaimValueTypes.Integer),
                        new Claim(ClaimTypes.Name, user.Username, ClaimValueTypes.String),                        
                        new Claim(ClaimTypes.Email, user.Email, ClaimValueTypes.String),
                        new Claim(ClaimTypes.GivenName, user.Name, ClaimValueTypes.String)
                    };
                    //ClaimsIdentity userIdentity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
                    ClaimsIdentity userIdentity = new ClaimsIdentity(claims, "login");
                    ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity);

                    await HttpContext.SignInAsync(principal);
                    return RedirectToAction("UserHome", "User");
                }
                else
                {
                    TempData["UserLoginFailed"] = "Login Failed. please enter correct data!";
                    return View();
                }
            }
            else
            {
                return View();
            }
        }

        [Authorize]
        public IActionResult UserHome()
        {
            return View();
        }

        [HttpGet]
        public async Task<IActionResult> Logout()
        {
            await HttpContext.SignOutAsync();
            return RedirectToAction("Login", "UserLogin");
        }
    }
Login View
       @model MVC_Login.Models.User
 @*<form asp-action="UserLogin">*@
        <form method="post" action="@Url.Action("UserLogin", "Login", new { Model })">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Username" class="control-label"></label>
                <input asp-for="Username" class="form-control" />
                <span asp-validation-for="Username" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Password" class="control-label"></label>
                <input asp-for="Password" class="form-control" />
                <span asp-validation-for="Password" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Login" class="btn btn-primary" />
                @*<a asp-action="Create" class="btn btn">Sign Up</a>*@
            </div>
        </form>


Is This A Good Question/Topic? 0
  • +

Page 1 of 1