用户注册和身份验证

时间:2013-12-28 14:55:44

标签: authentication authorization registration asp.net-mvc-5 asp.net-identity

我开发了一个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 。但是,我不明白我从中获得了什么好处。

另外,我不知道如何实现用户身份验证。

如果你告诉我一个进一步研究的载体,我会很高兴的。

1 个答案:

答案 0 :(得分:1)

你有2个选择。之一:

对于授权,Microsoft提供了一种称为基于声明的授权,它允许您定义用户和资源声明并基于它们定义授权约束。看看这里:Using Claim-Based Authorization

或者,您可以查看可扩展访问控制标记语言XACML。这将需要.NET框架之外的其他库。 XACML为您提供基于策略的细粒度授权。

HTH