[英]User registration and authentication
我开发一个Web应用程序。 它具有三层体系结构(数据访问层,业务逻辑层和表示层)。 数据访问层是使用NHibernate ORM(S#arp体系结构)实现的。 我有下表:
public partial class Role {
public Role()
{
this.Users = new Iesi.Collections.Generic.HashedSet<User>();
}
public virtual long Id
{
get;
set;
}
public virtual string Name
{
get;
set;
}
public virtual Iesi.Collections.Generic.ISet<User> Users
{
get;
set;
}
}
public partial class User {
public User()
{
this.Drivers = new Iesi.Collections.Generic.HashedSet<Driver>();
this.UserPhotos = new Iesi.Collections.Generic.HashedSet<UserPhoto>();
this.Roles = new Iesi.Collections.Generic.HashedSet<Role>();
}
public virtual long Id
{
get;
set;
}
public virtual string Login
{
get;
set;
}
public virtual string Email
{
get;
set;
}
public virtual string Salt
{
get;
set;
}
public virtual string Hash
{
get;
set;
}
public virtual string Name
{
get;
set;
}
public virtual Iesi.Collections.Generic.ISet<Driver> Drivers
{
get;
set;
}
public virtual University University
{
get;
set;
}
public virtual Iesi.Collections.Generic.ISet<UserPhoto> UserPhotos
{
get;
set;
}
public virtual Iesi.Collections.Generic.ISet<Role> Roles
{
get;
set;
}
}
public partial class Driver {
public Driver()
{
this.Trips = new Iesi.Collections.Generic.HashedSet<Trip>();
}
public virtual long Id
{
get;
set;
}
public virtual Car Car
{
get;
set;
}
public virtual User User
{
get;
set;
}
public virtual Iesi.Collections.Generic.ISet<Trip> Trips
{
get;
set;
}
}
系统中有一个用户 。 表驱动程序继承用户表。 系统中的每个用户可能具有多个角色。
我想实现一些事情。
1)用户注册。 这是实现此功能的正确方法吗?
[Authorize]
public class AccountController : Controller
{
private readonly IUserTasks userTasks;
public AccountController(IUserTasks userTasks)
{
this.userTasks = userTasks;
}
// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register()
{
return View();
}
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
public ActionResult Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var userToCreate = this.userTasks.Create(model);
return View(customerToCreate);
}
return View(model);
}
}
2)用户认证。 这是实现此功能的正确方法吗?
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = userTasks.Find(model.UserName, model.Password);
if (user != null)
{
///......
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
return View(model);
}
3)用户授权。 用户授权。 我想写一些属性,例如,
[Authorize(Roles = "Admin")]
public string Get(int id)
{
return "value";
}
我不知道怎么做。
在许多示例中,使用了新库Microsoft.AspNet.Identity 。 我应该使用它吗? 有一个实现NHibernate.AspNet.Identity 。 但是,我不明白我从中得到什么好处。
另外我也不知道如何实现用户认证。
如果您告诉我进一步研究的载体,我将非常高兴。
您有2个选择。 或者:
关于授权,Microsoft提供了一种称为基于声明的授权,它使您可以定义用户和资源声明并基于它们定义授权约束。 在这里看看: 使用基于声明的授权
另外,您可以研究XACML (可扩展访问控制标记语言)。 但是,这将需要.NET框架之外的其他库。 XACML为您提供基于策略的细粒度授权。
HTH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.