59

Tôi đang phát triển một ứng dụng ASP.NET MVC 5. Tôi có một DB hiện có, từ đó tôi đã tạo ra mô hình dữ liệu thực thể ADO.NET của mình. Tôi có một bảng trong DB đó có chứa "tên người dùng" và "mật khẩu" cột, và tôi muốn sử dụng chúng để thực hiện xác thực và ủy quyền trong Webapp của tôi; Tôi không thể tạo bất kỳ cơ sở dữ liệu hoặc bảng hoặc cột nào khác và tôi không thể sử dụng xác thực danh tính chuẩn vì yêu cầu của khách hàng. Tôi không cần quản lý đăng ký, thay đổi mật khẩu hoặc các nội dung khác: chỉ cần đăng nhập bằng mật khẩu và tên người dùng. Tôi có thể làm như thế nào?Cách triển khai xác thực tùy chỉnh trong ASP.NET MVC 5

Trả lời

112

Có, bạn có thể. Các phần xác thực và ủy quyền hoạt động độc lập. Nếu bạn có dịch vụ xác thực của riêng mình, bạn chỉ có thể sử dụng phần ủy quyền của OWIN. Hãy xem xét bạn đã có một UserManager xác nhận usernamepassword. Vì vậy, bạn có thể viết đoạn code sau trong bài action login trở lại của bạn:

[HttpPost] 
public ActionResult Login(string username, string password) 
{ 
    if (new UserManager().IsValid(username, password)) 
    { 
     var ident = new ClaimsIdentity(
      new[] { 
       // adding following 2 claim just for supporting default antiforgery provider 
       new Claim(ClaimTypes.NameIdentifier, username), 
       new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"), 

       new Claim(ClaimTypes.Name,username), 

       // optionally you could add roles if any 
       new Claim(ClaimTypes.Role, "RoleName"), 
       new Claim(ClaimTypes.Role, "AnotherRole"), 

      }, 
      DefaultAuthenticationTypes.ApplicationCookie); 

     HttpContext.GetOwinContext().Authentication.SignIn(
      new AuthenticationProperties { IsPersistent = false }, ident); 
     return RedirectToAction("MyAction"); // auth succeed 
    } 
    // invalid username or password 
    ModelState.AddModelError("", "invalid username or password"); 
    return View(); 
} 

Và quản lý người dùng của bạn có thể là một cái gì như thế này:

class UserManager 
{ 
    public bool IsValid(string username, string password) 
    { 
     using(var db=new MyDbContext()) // use your DbConext 
     { 
      // for the sake of simplicity I use plain text passwords 
      // in real world hashing and salting techniques must be implemented 
      return db.Users.Any(u=>u.Username==username 
       && u.Password==password); 
     } 
    } 
} 

Cuối cùng, bạn có thể bảo vệ những hành động hoặc điều khiển của bạn bằng cách thêm thuộc tính Authorize.

[Authorize] 
public ActionResult MySecretAction() 
{ 
    // all authorized users can use this method 
    // we have accessed current user principal by calling also 
    // HttpContext.User 
} 

[Authorize(Roles="Admin")] 
public ActionResult MySecretAction() 
{ 
    // just Admin users have access to this method 
} 
+0

Tôi rất tiếc, tôi là người mới có xác thực. Trường hợp shuold tôi sử dụng mã này? Làm thế nào tôi có thể yêu cầu MVC lấy các trường mật khẩu tên người dùng từ lớp EF của tôi? –

+4

Tôi vừa cập nhật bài đăng của mình để trả lời câu hỏi của bạn. –

+0

Cảm ơn! Nó hoạt động tuyệt vời! –

Các vấn đề liên quan