weixin_33739646 2016-06-11 08:54 采纳率: 0%
浏览 93

使用AJAX登录/注销

I am trying to login and logout using AJAX in ASP.Net MVC. When I register and login it works. After login it redirects me to my home page (which is correct) but it does not show the Welcome email and Logout link. What I am missing in code?

Contoller:

public ActionResult Index()
{
    return View();
}

[HttpGet]
public ActionResult LogIn()
{
    return View();
}

[HttpPost]
public ActionResult LogIn(Models.Registration userr)
{
    //if (ModelState.IsValid)
    //{
    if (IsValid(userr.Email, userr.Password))
    {
        FormsAuthentication.SetAuthCookie(userr.Email, false);
        return RedirectToAction("Index", "Home");
    }
    else
    {
        ModelState.AddModelError("", "Login details are wrong.");
    }
    return View(userr);
}

[HttpGet]
public ActionResult Register()
{
    return View();
}

[HttpPost]
public ActionResult Register(Models.Registration user)
{
    try
    {
       if (ModelState.IsValid)
       {
          using (var db = new ApartManagementSystem.DAL.ApartContext())
          {
             var crypto = new SimpleCrypto.PBKDF2();
             var encrypPass = crypto.Compute(user.Password);
             var newUser = db.Registrations.Create();
             newUser.Email = user.Email;
             newUser.Password = encrypPass;
             newUser.PasswordSalt = crypto.Salt;
             newUser.FirstName = user.FirstName;
             newUser.LastName = user.LastName;
             newUser.UserType = "User";
             newUser.CreatedDate = DateTime.Now;
             newUser.IsActive = true;
             newUser.IPAddress = "642 White Hague Avenue";
             db.Registrations.Add(newUser);
             db.SaveChanges();
             return RedirectToAction("Index", "Home");
          }
       }
       else
       {
          ModelState.AddModelError("", "Data is not correct");
       }
    }
    catch (DbEntityValidationException e)
    {
        foreach (var eve in e.EntityValidationErrors)
        {
           Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
           eve.Entry.Entity.GetType().Name, eve.Entry.State);
           foreach (var ve in eve.ValidationErrors)
           {
              Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                        ve.PropertyName, ve.ErrorMessage);
           }
        }
       throw;
     }
     return View();
}



@if (Request.IsAuthenticated)
{
   <strong>@Html.Encode(User.Identity.Name)</strong>
   @Html.ActionLink("Log Out", "LogOut", "User")
}
else
{
   @Html.ActionLink("Register", "Register", "User")
    <span> | </span>
    @Html.ActionLink("Log In", "LogIn", "User")
}
  • 写回答

1条回答 默认 最新

  • weixin_33727510 2016-06-11 10:08
    关注

    May be the Authentication ticket status is not saving in the context. Please check that you have something like this in your config file

    <system.web>
       <authentication mode="Forms">
           <forms loginUrl="Logon.aspx" name=".ASPXFORMSAUTH"></forms>
       </authentication>
    </system.web>
    

    and this block should be removed if it exist :

    <system.webServer>
       <modules>
        <remove name="FormsAuthentication" /> <----****
       </modules>
    </system.webServer>
    

    EDIT: here is where your webconfig file is locate enter image description here You can also check this link about configuration authentication Element (ASP.NET Settings Schema)

    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿