Tar*_*ied 8 asp.net authentication logging asp.net-mvc-3
我正在研究一个在公司中使用的项目.系统只有1个管理员帐户.
管理员将添加系统用户,每个用户都可以创建自己的联系人.
我创建了一个WCF服务来连接数据库,一个用于admin的asp.net mvc3项目,另一个用于系统用户的WPF应用程序.
我的问题是:
我只有一个用户(admin)用这个asp.net mvc项目登录:我该如何处理这种情况?
我认为不需要会员提供者和数据库,因为我只与一个用户合作,对吧?
试试这个:
web.config:
<authentication mode="Forms">
<forms loginUrl="~/Admin/LogOn" timeout="2880" >
<credentials passwordFormat="SHA1">
<user name="admin" password="5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8"/>
</credentials>
</forms>
</authentication>
Run Code Online (Sandbox Code Playgroud)
密码格式设置为SHA1,因此您的密码将不会以明文形式显示.它仍然可以破解.例如,使用在线SHA1生成器生成您自己的哈希.
loginUrl是您登录页面的路径(duh:P),如果不同则更改它.
CredentialsViewModel:
public class CredentialsViewModel
{
[Required]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
查看登录视图的模型.
AdminController:
public ViewResult LogOn()
{
return View();
}
[HttpPost]
public ActionResult LogOn(CredentialsViewModel model, string returnUrl)
{
if(ModelState.IsValid)
{
if(FormsAuthentication.Authenticate(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, false);
return Redirect(returnUrl ?? Url.Action("Index", "Admin"));
}
else
{
ModelState.AddModelError("", "Incorrect username or password");
}
}
return View();
}
[Authorize]
public ViewResult Index()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
因此,LogOn操作将验证从视图传递的凭据; 将它与web.config数据进行比较.
这里的重要部分是[授权]属性,该属性将阻止未经授权的用户访问.
归档时间: |
|
查看次数: |
1784 次 |
最近记录: |