2011-06-06 51 views
2

我们正在为客户构建一个ASP.NET MVC3 Web应用程序。将用户名/密码存储在会话状态以便进行一次性密码登录是否安全?

在此应用程序中,客户端希望用户使用他的用户名/密码组合登录。但是,如果用户名密码正确,则应将一次性引脚发送给用户的手机。用户只有进入正确的一次性引脚(OTP)后才能进行认证。

所以我想知道如果下面的解决方案是安全的:

  1. 用户用自己的用户名和密码登录。我们使用Membership.ValidateUser来验证用户名/密码,但我们尚未设置Auth Cookie。
  2. 我们在会话中存储用户的用户名和密码。
  3. 我们存储的事实是用户在会话中是“一半”登录的。
  4. 我们重定向回登录页面。
  5. 用户现在有机会进入OTP并再次提交给服务器。
  6. 我们验证用户名/密码/ OTP组合。
  7. 如果有效,我们设置表单身份验证Cookie。

PS:这一切都将发生在SSL上。

假设的登录页面可能看起来如下(注意三种状态):

<h2>Log On</h2> 
<div> 
    @if (User.Identity.IsAuthenticated) 
    { 
     <p class="green bold"> 
      You are logged-on fully. Your UserName and Password match, and the OTP you have entered was correct. 
     </p> 
     <form action="/Account/LogOff"> 
      <input type="submit" value="Log Off" /> 
     </form> 
    } 
    else if (ViewBag.AwaitingOTP) 
    { 
     <p> 
      Hi @ViewBag.UserName 
     </p> 
     <p class="orange"> 
      Step 2/2: Please enter the OTP sent to your cell phone. 
     </p> 
     <form method="post" action="/Account/VerifyOTPAndLogOn"> 
      <input name="otp" type="text" placeholder="One-Time Pin" /> 
      <input type="submit" /> 
     </form> 
    } 
    else 
    { 
     <p> 
      Hi stranger! 
     </p> 
     <p class="orange"> 
      Step 1/2: Please enter your username and password. 
     </p> 
     <form method="post" action="/Account/LogOnHalfwayAndRequestOTP"> 
      <input name="userName" type="text" placeholder="userName" /> 
      <input name="password" type="password" placeholder="password" /> 
      <input type="submit" value="Log On" /> 
     </form> 
    } 

</div> 

控制器代码如下:

public class AccountController : ControllerBase 
{ 

    public bool HalfwayLoggedOnStillAwaitingOTP 
    { 
     get 
     { 
      if (Session["AwaitingOTP"] != null) 
       return (bool)Session["AwaitingOTP"]; 

      return false; 
     } 
     set 
     { 
      Session["AwaitingOTP"] = value; 
     } 
    } 


    public ActionResult LogOn() 
    { 
     ViewBag.AwaitingOTP = HalfwayLoggedOnStillAwaitingOTP; 
     ViewBag.UserName = Session["UserName"] ?? null; 

     return View(); 
    } 


    public ActionResult LogOnHalfwayAndRequestOTP(string userName, string password) 
    { 
     //Authenticate user, but not fully... (i.e. we're not setting FormsAuthentication.SetAuthCookie yet) 
     if (Membership.ValidateUser(userName, password)) 
      HalfwayLoggedOnStillAwaitingOTP = true; 
     else 
      HalfwayLoggedOnStillAwaitingOTP = false; 

     ViewBag.AwaitingOTP = HalfwayLoggedOnStillAwaitingOTP; 
     ViewBag.UserName = Session["UserName"] ?? null; 

     //... 
     //...Call service that sends OTP to the user's cellPhone. 
     //... 

     return View("LogOn"); 
    } 


    public ActionResult VerifyOTPAndLogOn(string otp) 
    { 
     string userName = (string) Session["UserName"]; 
     string password = (string) Session["Password"]; 
     if (OTPIsValid(otp, userName, password)) 
     { 
      //Set the Forms Auth cookie... 
      FormsAuthentication.SetAuthCookie(userName, false); 

      return RedirectToAction("Index", "Home"); 
     } 
     else 
     { 
      //Display a nice error message here. 
      return View(); 
     } 


    } 

    private bool OTPIsValid(string otp, string userName, string password) 
    { 
     //... 
     //...Validate OTP here. For now we assume the user entered the correct OTP. 
     //... 
     return true; 
    } 
} 

是否有在此实现的任何安全漏洞?我不确定在会话中存储用户名/密码有多安全,或者如果在设置Session [“AwaitingOTP”]值时相信用户真的得到认证是安全的。

回答

1

我会用我过去24小时收集的信息回答我自己的问题。希望这是正确的。

让我担心的是我们提出的解决方案是我们仅仅依靠会话状态存储用户身份验证的短时间。 (因为只有在输入了OTP后,我们才会使用适当的表单身份验证)。

ASP.NET会话密钥存储在cookie中。它是加密的,所以相对安全。但它仍然是脆弱的标准窗体身份验证同样的威胁:

  • XSS
  • 人在这方面的中间人攻击(SSL将防止这一点)
  • 曲奇盗窃

我会改变建议的解决方案,不要在会话中存储用户的密码(并且只存储他的用户名)。我会确保使用SSL。

相关问题