2014-12-01 177 views
0

我正在制作一个使用MVC的个人项目,并希望实现身份验证功能。阅读一些书籍,谷歌搜索,它“出现”,有很多方式来正确认证用户,包括:与表单身份验证相混淆

  • 内置ASP.Net会员提供 - 我想避免这种情况。
  • 使用Cookie
  • 自定义窗体身份验证

我不知道为什么,但使用cookie似乎只是老我的脑海里自动认为它不安全。我已经链接了一些我一直在阅读的文章来帮助我。

我的过程中我自己的数据库来验证的基本认识是:

  • 用户呼叫帐控制器,它允许匿名访问(属性),用户输入
  • 查询数据库(我使用实体框架)
  • 如果用户找到了,那么FormsAuthentication.SetAuthCookie(username,false);否则显示错误。
  • 挂钩高达的FormsAuthentication.OnAuthenticate方法,做下面的代码项目的文章中类似的喜欢的东西:

代码:

protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e) 
{ 
    if (FormsAuthentication.CookiesSupported == true) 
    { 
     if (Request.Cookies[FormsAuthentication.FormsCookieName] != null) 
     { 

      try 
      { 
       //let us take out the username now     
       string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name; 
       string roles = string.Empty; 

       using (userDbEntities entities = new userDbEntities()) 
       { 
        User user = entities.Users.SingleOrDefault(u => u.username == username); 

        roles = user.Roles; 
       } 
       //let us extract the roles from our own custom cookie 


       //Let us set the Pricipal with our user specific details 
       e.User = new System.Security.Principal.GenericPrincipal(
        new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';')); 
      } 
      catch (Exception) 
      { 
       //somehting went wrong 
      } 
     } 
    } 
} 

搁笔:

formsauthentication.signout(). 

这是一个简单但SECURE认证的盒子标准方式?我并不太担心角色。最后,如果我使用授权属性或将不得不使我自己的属性这个上述工作。如果上述内容不正确或不安全,请告诉我需要什么。我不想使用成员资格提供程序数据库。我想用我自己的数据库来验证用户。

更新。多阅读一次后,它看起来像我还需要设置身份验证票证。虽然我不知道为什么。

See the article on CodeProject

谢谢加里我修复了这个链接。

回答