MVC在控制器中的认证
MVC在控制器中的认证
我们正在做一个有登录界面的网站。但是我们遇到了一个问题。我们的域名是localhost/Login/User。但是如果用户输入localhost/Home/Index,他/她可以在没有登录的情况下访问我们的主页。所以我们在我们的Index控制器中写了[Authorize]。但是我找不到我必须使用什么。我是否必须在我们的项目中使用AuthorizeAttribute?
#登录页面 public class LoginController : Controller { //GET: Login [IntranetAction] public ActionResult Users() { return View(); } public ActionResult Authentication(UserLoginInfo loginInfo) { bool isAuthenticated = new LdapServiceManager().isAuthenticated(loginInfo); if (isAuthenticated) { //已授权 Session["userName"] = loginInfo.username; return Redirect("/Home/Index"); } //密码错误,返回登录页面 TempData["message"] = "Yanlış kullanıcı adı ya da şifre"; return Redirect("/"); } }
主页
[Authorize] public ActionResult Index() { Session["ip"] = Request.UserHostAddress; if (IsDbExists()) { _contactList = new List(); UpdateOperations(); return View(_contactList); } Response.Redirect("/Loading/LoadingScreen"); return null; }
如何在我的LoginController/Authentication函数中访问Index页面?
MVC身份验证在控制器中的问题是由于需要允许用户在没有实际登录的情况下进行登录。为了解决这个问题,可以通过添加[AllowAnonymous]属性来允许未经身份验证的访问。
在代码中,可以创建一个名为AuthController的新控制器,并在其上添加[AllowAnonymous]属性。这样,用户就可以在没有实际登录的情况下进行登录。以下是一个在AuthController中使用[AllowAnonymous]属性的示例:
using System.Security.Claims; using System.Web; using System.Web.Mvc; using BusinessLogic.Services; using Common.Models; using Microsoft.AspNet.Identity; using Microsoft.Owin.Security; namespace Test.Controllers { [AllowAnonymous] public class AuthController : Controller { private readonly IUsersService _usersService; public AuthController(IUsersService usersService) { _usersService = usersService; } [HttpGet] public ActionResult LogIn() { return View(); } [HttpPost] public ActionResult LogIn(LoginModel loginModel) { if (!ModelState.IsValid) { return View(); } var isValid = _usersService.AuthenticateUser(loginModel); if (isValid) { var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, loginModel.Username), new Claim(ClaimTypes.Name, loginModel.Username), }, DefaultAuthenticationTypes.ApplicationCookie); Request.GetOwinContext().Authentication.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity); return Redirect(GetRedirectUrl(loginModel.ReturnUrl)); } ModelState.AddModelError("", "Invalid credentials"); return View(); } public ActionResult LogOut() { var ctx = Request.GetOwinContext(); var authManager = ctx.Authentication; authManager.SignOut("ApplicationCookie"); return RedirectToAction("index", "home"); } private string GetRedirectUrl(string returnUrl) { if (string.IsNullOrEmpty(returnUrl) || !Url.IsLocalUrl(returnUrl)) { return Url.Action("index", "home"); } return returnUrl; } } }
以上就是解决MVC身份验证在控制器中的问题的方法。添加[AllowAnonymous]属性可以允许未经身份验证的访问。下面是一些可能有用的参考链接:
- [ASP.NET Identity Stripped Bare - MVC Part 1](http://benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1)
- [Is FormsAuthentication obsolete?](https://softwareengineering.stackexchange.com/questions/284380/is-formsauthentication-obsolete)
- [Role-based access control (RBAC) vs. Claims-based access control (CBAC) in ASP.NET MVC](https://stackoverflow.com/questions/22814023)
- [.NET Security Cheat Sheet](https://www.owasp.org/index.php/.NET_Security_Cheat_Sheet)