c# - Logout doesnot return to the proper view -


in layout page have used request.isauthenticated method show login , logout link.login link poppup modal login form.( modal popup in partial page).

after logged in logout link show up. when hit logout l goes logoff method , redirect index action doesn't goes view instead shows same view. pleaseadvise me. have spend lots of hour fixed this

layout page

<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">         <div class="container">             <div class="navbar-header">                 <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">                     <span class="sr-only">toggle navigation</span>                     <span class="icon-bar"></span>                     <span class="icon-bar"></span>                     <span class="icon-bar"></span>                 </button>              </div>             <div id="navbar" class="navbar-collapse collapse">                 <ul class="nav navbar-nav">                     <li class="active"><a href="#">home</a></li>                     <li><a href="#">about</a></li>                      <li><a href="#">contact</a></li>                 </ul>                 <ul class="nav navbar-nav navbar-right">                     <li>                         @if (request.isauthenticated)                         {                             <div>                                    loggen as: @user.identity.name                                 </div>                             <a id="logout" style="cursor:pointer">                                 <span class="glyphicon glyphicon-log-out"></span> logout                             </a>                         }                          else                         {                             <div>                                 loggen as: @user.identity.name                                 </div>                                 <a style="cursor:pointer" data-toggle="modal" data-target="#loginmodal">                                     <span class="glyphicon glyphicon-log-in"></span> login                                 </a><h3>dsdasdasddsad</h3>                                 }   </li>                 </ul>             </div>         </div>     </nav>      <div id="loginmodal" class="modal fade" data-backdrop="static" data-keyboard="false" role="dialog">         <div class="modal-dialog">              <!-- modal content-->             <div class="modal-content">                 <div class="modal-header">                     <button type="button" class="close" data-dismiss="modal">&times;</button>                     <h4 class="modal-title">login</h4>                 </div>                 <div class="modal-body">                      @html.partial("_loginmodal")                  </div>                  <div class="modal-footer">                     <button id="login" type="button" class="btn btn-success">login</button>                     <button type="button" class="btn btn-success" data-dismiss="modal">close</button>                 </div>               </div>          </div>     </div>      <section id="main">         @renderbody()         <p>copyright w3schools 2012. rights reserved.</p>     </section>      <script type="text/javascript">          $('#logout').click(function () {              $.post("@url.action("logoff", "home")", function (data) {               });          });          $('#login').click(function (evt) {             evt.preventdefault();             var _this = $(this);             var _form = _this.closest("form");             var validator = $("form").validate();             var anyerror = false;              $('#loginform').find("input").each(function () {                 if (!validator.element(this)) {                     anyerror = true;                 }             });             if (anyerror)                 return false;             var form = $('#loginform').serialize();             $.ajax({                 type: "post",                 url: '/home/index',                 data: form,                 datatype: "json",                 success: function (response) {                      if (response.isredirect && response.isuser) {                         // alert('1');                         window.location.href = response.redirecturl;                     }                     else if (response.isredirect && response.isadmin) {                         // alert('2');                         window.location.href = response.redirecturl;                     }                      else if (!response.isredirect) {                         $('#loginform').find("#message").text(response.errormessage);                       }                  },                 error: function (response) {                     alert("some error");                 }              });            });     </script> 

partial page: inside shared folder

@model webapplication6.viewmodel.loginviewmodal @using webapplication6.viewmodel  <script src="~/scripts/jquery.validate.min.js"></script> <script src="~/scripts/jquery.validate.unobtrusive.min.js"></script> @using (html.beginform("index", "home", formmethod.post, new { @id = "loginform", @class = "form-horizontal" })) {     @html.validationsummary(true, "login failed. check login details")     <div class="form-group">         @html.labelfor(m => m.username, new { @class = "col-sm-3 control-label" })         <div class="col-sm-9">             @html.textboxfor(x => x.username, new { @class = "form-control", @placeholder = "enter username" })             @html.validationmessagefor(x => x.username)         </div>     </div>                           <div class="form-group">                             @html.labelfor(m => m.password, new { @class = "col-sm-3 control-label" })                             <div class="col-sm-9">                                 @html.passwordfor(x => x.password, new { @class = "form-control", @placeholder = "enter username" })                                 @html.validationmessagefor(x => x.password)                             </div>                         </div>                          <div class="form-group">                             @html.label("user type", new { @class = "col-sm-3 control-label" })                             <div class="col-sm-3">                                 @html.dropdownlist("usertype",            new selectlist(enum.getvalues(typeof(usertype))), new { @class = "form-control" })                             </div>                         </div>                          <div class="form-group">                              <div id="message"  style="color:#ff0000; font-weight:bold;" class="col-sm-offset-3 col-sm-12">                              </div>                          </div>   } 

controller:

using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using system.web.security; using webapplication4.helper; using webapplication6.models; using webapplication6.viewmodel;  namespace webapplication6.controllers {     public class homecontroller : controller     {           // get: home         public actionresult index()         {             return view();         }             [httppost]         public jsonresult index(loginviewmodal lm)         {             caremanagemententities db = new caremanagemententities();             if (modelstate.isvalid)             {                 var useractive = db.usrs.where(x => x.usr_nm == lm.username && x.isactive == true && x.isdeleted == false).firstordefault();                 if (useractive != null)                 {                     var userss = db.usrs.where(x => x.usr_nm.tolower() == lm.username.tolower()).firstordefault();                     var validpassword = passwordhelper.validatepassword(lm.password, userss.usr_pwd);                     if (validpassword)                     {                         var users = db.usrs.where(x => x.usr_nm == lm.username).firstordefault();                         if (users != null)                         {                             var userwithrole = db.usrs.where(x => x.usr_nm == lm.username && x.usr_role == lm.usertype.tostring()).firstordefault();                             if (userwithrole != null)                             {                                 if (userwithrole.usr_role == "admin")                                 {                                     formsauthentication.setauthcookie(lm.username, false);                                     return json(new                                     {                                          redirecturl = "/admin/index",                                         isredirect = true,                                         isadmin = true,                                     });                                 }                                  else if (userwithrole.usr_role == "user")                                 {                                     return json(new                                     {                                         redirecturl = "/userpage/index",                                         isredirect = true,                                         isuser = true,                                     });                                 }                                 else                                 {                                     return json(new                                     {                                         isredirect = false,                                         errormessage = "the user name or password provided incorrect."                                     });                                  }                              }                              else                             {                                 return json(new                                 {                                     isredirect = false,                                     errormessage = "the user name or password provided incorrect."                                 });                             }                         }                      }                      else                     {                         return json(new                         {                             isredirect = false,                             errormessage = "the user name or password provided incorrect."                         });                      }                   }                 else                 {                     return json(new                     {                         isredirect = false,                         errormessage = "the user name or password provided incorrect."                     });                  }             }              return json(new             {                 isredirect = false,                 errormessage = "error happen please reload page"             });           }           public actionresult logoff()         {             formsauthentication.signout();             session.abandon();              // clear authentication cookie             httpcookie cookie1 = new httpcookie(formsauthentication.formscookiename, "");             cookie1.expires = datetime.now.addyears(-1);             response.cookies.add(cookie1);              // clear session cookie (not necessary current problem recommend anyway)             httpcookie cookie2 = new httpcookie("asp.net_sessionid", "");             cookie2.expires = datetime.now.addyears(-1);             response.cookies.add(cookie2);              formsauthentication.redirecttologinpage();             return redirecttoaction("index", "home");         }      } } 

i making ajax call , ajax calls stay on same page. replace ajax call action link works me.

thank stephen muecke quick right answer.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -