Chuyển đến nội dung chính

[ASP.NET MVC] Authentication và Authorize

Một trong những vấn đề bảo mật cơ bản nhất là đảm bảo những người dùng hợp lệ truy cập vào hệ thống. ASP.NET đưa ra 2 khái niệm: Authentication và Authorize
Authentication xác nhận bạn là ai. Ví dụ: Bạn có thể đăng nhập vào hệ thống bằng username và password hoặc bằng ssh.
Authorization xác nhận những gì bạn có thể làm. Ví dụ: Bạn được phép truy cập vào website, đăng thông tin lên diễn đàn nhưng bạn không được phép truy cập vào trang mod và admin.

Hạn chế truy cập với Authorize Attribute

ASP.NET MVC cung cấp filter AuthorizeAttribute, được thực hiện trước khi vào controller.
 <authentication mode="Forms">
 <forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
Với form auuthentication được kích hoạt, nếu người dùng cố gắng truy cập vào một trang cần được xác thực, họ sẽ được chuyển hướng đến trang login (Account/Login) nhập tên người dùng và mật khẩu
Để yêu cầu xác thực 1 trang, bạn cần đặt AuthorizeAttribute trước Action cần kiểm tra:
 [AuthorizeAttribute]
public ActionResult About()
{
 return View();
}
Hoặc bạn có thể ghi ngắn gọn hơn:
 [Authorize]
public ActionResult About()
{
 return View();
}
Để lưu lại trang thái đăng nhập của người dùng, bạn dùng hàm FormsAuthentication.SetAuthCookie(username, rememberme)
Khi log out ra khỏi hệ thống, bạn dùng hàm FormsAuthentication.SignOut() để xóa cookie.
Đây là đoạn code hoàn chỉnh:
[Authorize]
public class AccountController : Controller
{
 private UserRepository _userRepository;

 public AccountController()
 {
  _userRepository = new UserRepository();
 }
 //
 // GET: /Account/Login

 [AllowAnonymous]
 public ActionResult Login(string returnUrl)
 {
  ViewBag.ReturnUrl = returnUrl;
  return View();
 }

 //
 // POST: /Account/Login

 [HttpPost]
 [AllowAnonymous]
 public ActionResult Login(LoginModel model, string returnUrl)
 {
  if (_userRepository.Validate(model.UserName, model.Password))
  {
   FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
   return RedirectToLocal(returnUrl);
  }
  // If we got this far, something failed, redisplay form
  ModelState.AddModelError("", "The user name or password provided is incorrect.");
  return View(model);
 }

 //
 // POST: /Account/LogOff

 [HttpPost]
 public ActionResult LogOff()
 {
  //WebSecurity.Logout();
  FormsAuthentication.SignOut();
  return RedirectToAction("Index", "Home");
 }

 private ActionResult RedirectToLocal(string returnUrl)
 {
  if (Url.IsLocalUrl(returnUrl))
  {
   return Redirect(returnUrl);
  }
  else
  {
   return RedirectToAction("Index", "Home");
  }
 }        
}
public class UserRepository
{
 private static List<User> _userList = new List<User>
 {
  new User{ Id = 1, Name = "admin", Password = "123"},
  new User{ Id = 2, Name = "anbinhtrong", Password = "123"}
 };

 public bool Validate(string username, string password)
 {
  var user = _userList.FirstOrDefault(t => t.Name == username);
  if (user == null)
   return false;
  if (user.Password == password) return true;
  return false;
 }
}
Chúc các bạn thành công.

Nhận xét

Bài đăng phổ biến từ blog này

ASP.NET MVC: Cơ bản về Validation

Validation (chứng thực) là một tính năng quan trọng trong ASP.NET MVC và được phát triển trong một thời gian dài. Validation vắng mặt trong phiên bản đầu tiên của asp.net mvc và thật khó để tích hợp 1 framework validation của một bên thứ 3 vì không có khả năng mở rộng. ASP.NET MVC2 đã hỗ trợ framework validation do Microsoft phát triển, tên là Data Annotations. Và trong phiên bản 3, framework validation đã hỗ trợ tốt hơn việc xác thực phía máy khách, và đây là một xu hướng của việc phát triển ứng dụng web ngày nay.

Tổng hợp một số kiến thức lập trình về Amibroker

Giới thiệu về Amibroker Amibroker theo developer Tomasz Janeczko được xây dựng dựa trên ngôn ngữ C. Vì vậy bộ code Amibroker Formula Language sử dụng có syntax khá tương đồng với C, ví dụ như câu lệnh #include để import hay cách gói các object, hàm trong các block {} và kết thúc câu lệnh bằng dấu “;”. AFL trong Amibroker là ngôn ngữ xử lý mảng (an array processing language). Nó hoạt động dựa trên các mảng (các dòng/vector) số liệu, khá giống với cách hoạt động của spreadsheet trên excel.