locked
how to update the password for login in mvc? RRS feed

  • Question

  • User835758924 posted

    I want to update the password for login

    what I am doing in my application when user login then after seeing their own profile

    student.cs

    namespace Demo.Models
    {
        public class student
        {
            public int studentid { get; set; }
    
            [Display(Name = "FirstName")]
            public string firstname { get; set; }
    
            [Display(Name = "LastName")]
            public string lastname { get; set; }
    
            [Display(Name = "UserName")]
            public string username { get; set; }
    
            [Display(Name = "Password")]
            public string password { get; set; }
    
            [Display(Name = "Emailid")]
            public string emailid { get; set; }
        }

    HomeController.cs

            public ActionResult Index()
            {
                if (Session["username"] == null)
                {
                    return RedirectToAction("Login", "Home");
                }
                else
                {
                    return View(getcurrentstudent());
                }
            }
    
            public student getcurrentstudent()       
            {
                var currentusername = Session["username"].ToString();
                var currentpassword = Session["password"].ToString();
                var currentstu = _dbcontextstud.stud.Where(s => s.username == currentusername && s.password == currentpassword).SingleOrDefault();
    
                return currentstu;
            }
            public ActionResult Login()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Login(student stud)
            {
                var login = _dbcontextstud.stud.Where(x => x.username == stud.username && x.password == stud.password).FirstOrDefault();
    
                if (login != null)
                {
                    Session["username"] = login.username.ToString();
                    Session["password"] = login.password.ToString();
    
                    return RedirectToAction("Index");
                }
    
                return RedirectToAction("Login");
            }
    
            public ActionResult create()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult create(student stud)
            {
                var create = _dbcontextstud.stud.Add(stud);
                _dbcontextstud.SaveChanges();
                return RedirectToAction("Login");
            }

    Index.cshtml

    @model Demo.Models.student
    
    <h2>Index</h2>
    
    @if (Session["username"] != null)
    {
        <text>Welcome <strong>@Session["username"].ToString()</strong></text>
    }
    
    <p>
    
        @Html.ActionLink("Logout", "Logout")
    
    </p>
    <table class="table">
        <tr>
            <td>
                @Html.DisplayFor(model => model.firstname)
            </td>
            <td>
                @Html.DisplayFor(model => model.lastname)
            </td>
            <td>
                @Html.DisplayFor(model => model.username)
            </td>
            <td>
                @Html.DisplayFor(model => model.password)
            </td>
            <td>
                @Html.DisplayFor(model => model.emailid)
            </td>
        </tr>
    </table>

    database 

    see reference when I log in:

    how to update the password for login in MVC?

    please help

    Monday, July 20, 2020 11:25 AM

All replies