vishnu vishnu

vishnu vishnu

  • 1.6k
  • 287
  • 4.6k

Form Athentication(mutliple contoller not working)

Aug 16 2019 11:32 PM
Hi i have Created form athentciation
  1. <authentication mode ="Forms">  
  2. <forms loginUrl="~/Home/Index" timeout="2880" enableCrossAppRedirects="false"/>
  3. </authentication>  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Security;  
  7. namespace Role_Menu_MVC.Controllers  
  8. {  
  9. public class HomeController : Controller  
  10. {  
  11. [AllowAnonymous]  
  12. public ActionResult Index()  
  13. {  
  14. return View();  
  15. }  
  16. [AllowAnonymous]  
  17. public ActionResult Index1()  
  18. {  
  19. return View();  
  20. }  
  21. [Authorize]  
  22. public ActionResult Profile()  
  23. {  
  24. return View();  
  25. }  
  26. [HttpPost]  
  27. [AllowAnonymous]  
  28. public ActionResult Index(User user)  
  29. {  
  30. UsersEntities usersEntities = new UsersEntities();  
  31. RoleUser roleUser = usersEntities.ValidateUser(user.Username, user.Password).FirstOrDefault();  
  32. string message = string.Empty;  
  33. switch (roleUser.UserId.Value)  
  34. {  
  35. case -1:  
  36. message = "Username and/or password is incorrect.";  
  37. break;  
  38. case -2:  
  39. message = "Account has not been activated.";  
  40. break;  
  41. default:  
  42. FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user.Username, DateTime.Now, DateTime.Now.AddMinutes(2880), user.RememberMe, roleUser.Roles, FormsAuthentication.FormsCookiePath);  
  43. string hash = FormsAuthentication.Encrypt(ticket);  
  44. HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);  
  45. FormsAuthentication.SetAuthCookie(user.Username,true);  
  46. if (ticket.IsPersistent)  
  47. {  
  48. cookie.Expires = ticket.Expiration;  
  49. }  
  50. Response.Cookies.Add(cookie);  
  51. if (!string.IsNullOrEmpty(Request.Form["ReturnUrl"]))  
  52. {  
  53. return RedirectToAction(Request.Form["ReturnUrl"].Split('/')[2]);  
  54. }  
  55. else  
  56. {  
  57. return RedirectToAction("Index","Profile");  
  58. }  
  59. }  
  60. ViewBag.Message = message;  
  61. return View(user);  
  62. }  
  63. [HttpPost]  
  64. [Authorize]  
  65. public ActionResult Logout()  
  66. {  
  67. FormsAuthentication.SignOut();  
  68. return RedirectToAction("Index");  
  69. }  
  70. }  
  71. }  
  72. [Authorize]  
  73. public class AdminController : Controller  
  74. {  
  75. // GET: Admin  
  76. [HttpPost]  
  77. public ActionResult Index()  
  78. {  
  79. return View();  
  80. }  
  81. }  
i am truy to navigate <li>@Html.ActionLink("Admin", "Index")</li>
 
but its going to login page
 
please help naviagation with html link not working .when click anther controller

Answers (1)