【问题标题】:ASP.NET MVC custom membership for beginners面向初学者的 ASP.NET MVC 自定义成员资格
【发布时间】:2011-04-12 15:53:47
【问题描述】:

我正在创建自己的网站和博客,我希望第一次只有我自己在数据库中(我的姓名和密码),以后可能会为其他人注册,但首先只为我登录并获得授权管理。我不想使用 MS 的会员资格。我想尝试从一开始就创建自己的,所以我正在寻找初学者指南,但我找到了具有角色和权利的大型指南。我只想用登录数据检查数据库中的用户名、密码的小例子。 感谢帮助 伦敦银行同业拆借利率

【问题讨论】:

  • 您好,您是否已经选择了一种访问数据库的方式? (实体框架、linq2sql、ado.net?)

标签: asp.net-mvc membership provider


【解决方案1】:

嘿@Bibo,最好不要选择会员提供者。我认为 UserService 或类似的提供创建、验证用户的方法和其他一些方法就足够了。作为建议,使用密码散列和密码盐作为用户密码。 Here 是一个很好的链接。也可以看看我前段时间给的this answer

祝你好运!

编辑: rememberMe 参数应该命名为 keepMeSignedIn。

【讨论】:

  • 感谢您的回答,但我找不到任何可以帮助我的东西。你能链接一些关于那个的文章吗?
  • CodeCampServer codecampserver.codeplex.com 是一个示例应用程序,可实现您正在寻找的内容。
【解决方案2】:

这篇关于表单身份验证的文章为您提供了大量信息,用于创建您自己的简单安全系统,尤其是有关 FormsAuthenticationTicket 的部分。

http://support.microsoft.com/kb/301240

【讨论】:

    【解决方案3】:

    即使您不想使用成员资格和角色提供者数据存储,您仍然可以使用身份验证。相信我,这比构建自己的要容易得多。以下是它的工作原理:

    我们会说您已经设置了用于检索用户名和密码的用户存储空间。为了简单起见,我将假设您有一个名为 DataLayer 的静态类,其中包含用于从数据库(或您使用的任何存储)中提取信息的数据检索方法。

    首先,您需要一种让用户登录的方法。因此,请设置一个包含用户名和密码字段的页面。然后在页面发布的action方法中设置一个快速if语句:

        if (DataLayer.UserExists(userModel.Username))
        {
             User userFromDB = DataLayer.GetUser(userModel.Username);
             if (userFromDB.Password == userModel.Password)
             {
                  FormsAuthentication.SetAuthCookie(userFromDB.Username, checkBoxRememberMe.Checked);
                  //Use userFromDB as the username to authenticate because it will 
                  //preserve capitalization of their username the way they entered it
                  //into the database; that way, if they registered as "Bob" but they
                  //type in "bob" in the login field, they will still be authenticated
                  //as "Bob" so their comments on your blogs will show their name
                  //the way they intended it to.
    
                  return "Successfully logged in!";
             }
        }
    
        return "Invalid username or password.";
    

    现在他们已通过身份验证,您可以在代码中使用 Page.User.Identity.IsAuthenticated 来确定他们是否已登录。像这样:

    if (User.Identity.IsAuthenticated)
    {
         DataLayer.PostBlogComment(User.Identity.Name, commentBody);
         //Then in your controller that renders blog comments you would obviously 
         //have some logic to get the user from storage by the username, then pull
         //their avatar and any other useful information to display along side the
         //blog comment. This is just an example.
    }
    

    此外,您可以将整个操作方法甚至整个控制器锁定给通过表单身份验证提供程序进行身份验证的用户。您所要做的就是将这些标签添加到您的操作方法/控制器中:

    [Authorize]
    public ActionResult SomeActionMethod()
    {
        return View();
    }
    

    [Authorize] 属性将阻止未登录的用户访问该操作方法,并将他们重定向到您的登录页面。如果您使用的是内置角色提供程序,则可以使用相同的属性过滤掉角色。

    [Authorize(Roles="Admin, SalesReps")]
    public ActionResult SomeActionMethod()
    {
        return View();
    }
    

    这些属性也可以添加到控制器类之上,以将其逻辑应用于整个控制器。

    编辑:要注销用户,您只需拨打FormsAuthentication.SignOut();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多