Click here to Skip to main content
15,041,266 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this in my controller for both logging in and logging out

In this part I am creating the part so that the user can log in through permissions
ASP.NET
[HttpPost]
     public ActionResult Login(registro model, string returnUrl)
    {
        try
        {
            masterEntities db = new masterEntities();
        var dataItem = db.registro.Where
       (x => x.usuario == model.usuario && x.clave == model.clave).First();
        
        if (dataItem != null)
        {
            FormsAuthentication.SetAuthCookie(dataItem.usuario, false);
            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                     && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
            {

                return Redirect(returnUrl);
            }


            else
            {
                return RedirectToAction("Index");
            }

        }
        else
        {
            ModelState.AddModelError("", "Invalid user/pass");
            return View();
        }
        }
        catch (Exception es)
        {
         
      Response.Write (
      "<script>alert('Usuario ó contraseña no valido')</script>");
            return View("../Home/Login");

            throw es;               
        }
        }

And in this part so that the user can log out
<pre lang="ASP.NET">
 [Authorize]
    public ActionResult SignOut()
    {
        FormsAuthentication.SignOut();
        return RedirectToAction("Login", "Home");
    }


Here I am already creating the login.cshtml view where all the data is inserted so that the user can log in to the .cshtml page:


ASP.NET
@model WebApplication1.Models.registro
             @{
           ViewBag.Title = "Login";
              }

      <!DOCTYPE html>

        <html>
       <head>
     <title>Iniciar sesion</title>

      <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" 
       rel="stylesheet" id="bootstrap-css">
     <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
     <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>

    <link href="@Url.Content("~/Content/Login.css")" rel="stylesheet" type="text/css">
            </head>
           <body>
       @using (Html.BeginForm("Login", "Home",
          new { returnUrl = Request.QueryString["ReturnUrl"] }, FormMethod.Post))
     {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <div class="container-fluid">
        <div class="row">
            <p><p><p>
                <div class="img.izquierda">
                    <img src="" />
                </div>
                <div class="col-xs-12 col-sm-6 col-md-8 lg-bg"></div>

                <div class="col-xs-6 col-md-4">

                    <div class="logo-text"></div>
                    <div class="logo-area">
                        <img src="" />
                    </div>
                    <div class="form-group">

                        <label for="exampleInputEmail1">Nombre de usuario</label>
                        @Html.EditorFor(model => model.usuario, new { htmlAttributes = new { 
                    @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.usuario, "", new { @class = 
                    "text-danger" })



                    </div>
                    <div class="form-group">
                        <label for="exampleInputPassword1">Contraseña</label>

                        @Html.EditorFor(model => model.clave, new { htmlAttributes = new { 
                     @class = "form-control", @type = "password" } })
                        @Html.ValidationMessageFor(model => model.clave, "", new { @class = 
                     "text-danger" })

                    </div>
                    <button type="submit" class="btn btn-success">Login</button>

                </div>
                  </div>

                </div>

       }


      </body>
          </html>


And here I send it to call from the web.config the part where I set the user for a while
ASP.NET
<authentication mode="Forms">
  <forms loginUrl="~/Home/Login" defaultUrl="~/" timeout="20" slidingExpiration="true" />
</authentication


What I have tried:

My question is about how I can make the moment I give it remember user in the checkbox, and I close the session, it shows me the username and password in the 2 textboxes and if I do not remember me at the moment of closing the session, leave the 2 textboxes blank , since I don't know how to do it and I'm new to asp.net mvc
Posted
Updated 9-Jun-21 21:39pm

1 solution

Basically, you don't.

All modern browsers have built-in password managers. The user can choose to save their password when they sign in, and let the browser's password manager fill in their username and password when they come back.

Any attempt to write code to auto-populate the credentials will introduce massive security problems.

Troy Hunt: How to build (and how not to build) a secure “remember me” feature[^]
   

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