使用AJAX登录/注销

时间:2016-06-11 08:54:30

标签: ajax

我正在尝试使用ASP.Net MVC中的AJAX登录和注销。当我注册并登录它的工作原理。登录后,它将我重定向到我的主页(这是正确的)但它没有显示欢迎电子邮件和注销链接。我在代码中缺少什么?

位指示:

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 个答案:

答案 0 :(得分:0)

可能是身份验证票证状态未在上下文中保存。请在配置文件中检查您是否有类似的内容

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

如果存在,则应删除此块:

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

编辑:这是您的webconfig文件所在的位置enter image description here 您还可以查看有关配置authentication Element (ASP.NET Settings Schema)

的此链接