2016-06-11 135 views
1

我想在ASP.Net MVC中使用AJAX登录和注销。当我注册并登录它的作品。登录后,它将我重定向到我的主页(这是正确的),但它不显示欢迎电子邮件和注销链接。我在代码中缺少什么?使用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") 
} 

回答

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)

+0

首先我添加web.config?我试过,我couldnt add.It给出错误:Xml documnent不能包含根级元素 – yiad

+0

我更新我的答案。在Asp.net mvc项目中,根文件夹中有一个名为web.config的文件。你必须打开并编辑它。无需创建新的配置文件。 – Tchaps