我们正在为客户构建一个ASP.NET MVC3 Web应用程序。将用户名/密码存储在会话状态以便进行一次性密码登录是否安全?
在此应用程序中,客户端希望用户使用他的用户名/密码组合登录。但是,如果用户名密码正确,则应将一次性引脚发送给用户的手机。用户只有进入正确的一次性引脚(OTP)后才能进行认证。
所以我想知道如果下面的解决方案是安全的:
- 用户用自己的用户名和密码登录。我们使用Membership.ValidateUser来验证用户名/密码,但我们尚未设置Auth Cookie。
- 我们在会话中存储用户的用户名和密码。
- 我们存储的事实是用户在会话中是“一半”登录的。
- 我们重定向回登录页面。
- 用户现在有机会进入OTP并再次提交给服务器。
- 我们验证用户名/密码/ OTP组合。
- 如果有效,我们设置表单身份验证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”]值时相信用户真的得到认证是安全的。