2015-03-25 127 views
4

我是一个红宝石的家伙,我是新手在asp.net,所以我请裸露..如何在mvc.net中实现安全认证和角色机制?

我想在我的第一个mvc应用程序中实现角色机制,所以我可以使用数据注释( [授权(角色=“管理员”)])以上actionResults ..

我读了很多关于安全身份验证,成员和角色,仍然无法解决所有这些问题。例如,我读到这里:

http://stackoverflow.com/questions/10742709/asp-net-mvc4-security-authentication-and-authorization

甚至试图复制以下: http://www.codeproject.com/Articles/654846/Security-In-ASP-NET-MVC

我实现了roleProvider和AccountMembershipProvider接口,但我必须在这里失去了一些东西必不可少的,因为它不是工作..

我需要一个很好的教程,我需要实施以及如何组合所有。

你能向我解释一下如何实现这个目标吗?

干杯。

+1

你没有提到任何有关web.config的内容。当有人试图登录到应用程序时,您需要告诉您的应用程序使用AccountMembershipProvider和RoleProvider。你尝试过吗? – 2015-03-25 10:26:25

+0

可以详细说明一下吗? – KingOnRails 2015-03-25 11:10:41

回答

5

我建立了本教程一段时间.. 它结合了几个来源的现有教程..所以,也许你已经看到他们的一部分。

为了使其工作,您必须在您的应用程序中实现以下所有代码。

让我们开始吧:

当你的应用程序的web.config文件具有下<system.web>标签下面的章节中的数据注解机制被激活:

<system.web> 

    <!-- authentication section activates the auth system --> 

    <authentication mode="Forms"> 
     <forms loginUrl="~/yourLoginControllerName/YourLoginActionResult" timeout="2880" /> 
    </authentication> 

    <!-- membership section defines which class is used to check authentication, in this example, this is the default class --> 

    <membership defaultProvider="AccountMembershipProvider"> 
     <providers> 
     <clear/> 
     <add name="AccountMembershipProvider" 
      type="yourProjectName.Web.Infrastructure.AccountMembershipProvider" /> 
     </providers> 
    </membership> 

    <!-- roleManager section defines which class is used to check roles for users, in this example, the default class is used --> 

    <roleManager enabled="true" defaultProvider="AccountRoleProvider"> 
     <providers> 
     <clear/> 
     <add name="AccountRoleProvider" 
      type="yourProjectName.Web.Infrastructure.AccountRoleProvider" /> 
     </providers> 
    </roleManager> 
.. 
    </system.web> 

装饰可以让你控制哪些角色可以访问哪个控制器操作,并定义控制器操作是否需要身份验证才能访问它们,或者是否可以由所有用户访问。控制由控制器中的以下属性完成,例如:

public class HomeController : Controller 
{ 
    [Authorize] 
    public ActionResult Index() 
    { 
.. 
    } 

    [Authorize(Roles = "Administrator, KingOnRails")] 
    public ActionResult Edit(int Id) 
    { 
.. 
    } 

默认系统限制您按原样使用数据注释。它需要数据库连接来创建用户表和角色。

以下内容向您展示了如何覆盖它,并将成员资格和角色管理类替换为可以使用您自己的第三方库对用户进行身份验证和授予角色的简单类。

您需要实现以下2类,为了更好的可读性,把它们放在同一个文件夹在您的解决方案:

public class AccountMembershipProvider : MembershipProvider 
{ 
     public override bool ValidateUser(string username, string password) 
     { 
      if (username == "KingOnRails") 
       return true; 
      return false; 
     } 
} 

和:

public class AccountRoleProvider : RoleProvider 
{ 

     public override void AddUsersToRoles(string[] usernames, string[] roleNames) 
     { 
       //Here you can implement insertion of <key, value> = <user, role> to a global dictionary maintained in Global.asax file... 
     } 

     public override string[] GetRolesForUser(string username) 
     { 
      if (username == "Roy Doron") 
       return new string[1] { "User" }; 
      else if (username == "KingOnRails") 
       return new string[1] { "Administrator" }; 
      return null; 
     } 

     public override bool RoleExists(string roleName) 
     { 
      if ((roleName == "Administrator") || (roleName == "User")) 
       return true; 
      else 
       return false; 
     } 
} 

这是对的..现在所有你需要在自己的登录控制器中编写自己的流程,如果没有,你需要实现一个登录页面。

当用户尝试登录到您的系统时,登录控制器应调用成员资格ValidateUser()方法,如果成功,则需要为该用户创建Web表单身份验证票据,然后将用户重定向到您想要,例如:

[HttpPost] 
public ActionResult Login() 
     { 
      string user = Request.Params["user"]; 
    // calls the AccountMembershipProvider.ValidateUser() 
      if (Membership.ValidateUser(user, Request.Params["password"])) 
      { 
       FormsAuthentication.SetAuthCookie(user, true); 
       return Redirect("/Home/WhereEver"); 
      } 
      else 
       return Redirect("/Home/Login"); 

     } 

这就是希望它有所帮助。

如果您有任何其他问题,请随时询问。

祝你好运。