0

Ich lese durch die folgenden answer, ich denke, es sollte gearbeitet werden, aber wo und welchen Teil in diesen Kommentar zu setzen?Set Formular Authentifizierung Cookies abgelaufene Zeit

HttpCookie authCookie = FormsAuthentication.GetAuthCookie(username, isPersistent); 
if (!isPersistent) 
{ 
    //this is because if it was not set then it got 
    //automatically set to expire next year even if 
    //the cookie was not set as persistent 
    authCookie.Expires = DateTime.Now.AddMinutes(15); 
} 

Response.Cookies.Add(authCookie); 

Ist es in der global.asax oder Controller selbst?

Unten sind meine Codes.

Von Controller-Ansicht:

[HttpPost] 
    public ActionResult Login(User user, string returnUrl) 
    { 
     if (ModelState.IsValid) 
     { 
      var username = user.Username; 
      var getPassword = (from item in db.User 
           where item.Username == username 
           select new UserModel() 
           { 
            Password = item.Password 
           } 
          ).SingleOrDefault(); 

      if (getPassword != null) 
      { 
       var hashingPass = Models.PasswordHash.ValidatePassword(user.Password, getPassword.Password); 
       var getAdmin = (from item in db.User 
           where item.Username == username && hashingPass == true 
           select new UserModel() 
           { 
            UserId = item.UserId 
           } 
           ).ToList(); 
       if (getAdmin.Count.Equals(1)) 
       { 
        FormsAuthentication.SetAuthCookie(username, false); 
        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) 
        { 
         return Redirect(returnUrl); 
        } 
        else 
        { 
         return RedirectToAction("Index"); 
        } 
       } 
       else 
       { 
        ModelState.AddModelError("", "The username or password provided is incorrect."); 
       } 
      } 
      else 
      { 
       ModelState.AddModelError("", "The username or password provided is incorrect."); 
      } 
     } 
     return View(user); 
    } 

Von HTML-Ansicht:

@using (Html.BeginForm()) 
        { 
        @Html.ValidationSummary(true) 
        <form role="form"> 
         <fieldset> 
          <div class="form-group"> 
           <label for="Username">Username</label> 
           <input class="form-control" placeholder="Enter Username" name="Username" id="Username" type="text" autofocus oninput="setCustomValidity('')" required/> 
          </div> 
          <div class="form-group"> 
           <label for="Password">Password</label> 
           <input class="form-control" placeholder="Enter Password" name="Password" id="Password" type="password" value="" oninput="setCustomValidity('')" required> 
          </div> 
          <button type="submit" style="background-color:#f7aa52; border:1px solid #f78952; color:#fff;" class="btn btn-lg btn-block">Login</button> 
         </fieldset> 
        </form> 
        } 

Antwort

0

Es ist in der Controller-Ebene.

Sie können auch den Cookie-Ablauf in web.config unter ya

<system.web> 
    <authentication mode="Forms"> 
      <forms timeout="50000000" slidingExpiration="true"/> 
    </authentication> 
</system.web> 
+0

angeben, habe ich versucht, aber es wird nicht erzwingen, dass Benutzer – Tss

+0

Prüfung dieses mit 10.000 Millisekunden abzuzumelden ~ 10 Sekunden. –

+0

zeigt den Benutzer-UI-Code an. –