asp.net-mvc – web.config中的表单身份验证
发布时间:2020-05-23 17:16:23 所属栏目:asp.Net 来源:互联网
导读:我正在使用MVC3,并将用户认证放在web.config文件中。这是绕过sqlserver身份验证。 代码如下web.config中所示: authentication mode=Forms forms loginUrl=~/Account/LogOn timeout=2880 credentials passwordFormat=Clear
|
我正在使用MVC3,并将用户认证放在web.config文件中。这是绕过sqlserver身份验证。 代码如下web.config中所示: <authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" >
<credentials passwordFormat="Clear">
<user name="test123" password="test123" />
</credentials>
</forms>
</authentication>
我尝试使用提到的用户标识和密码进行登录,我在页面中出现错误 登录失败。请更正错误,然后重试。 * The user name or password provided is incorrect. 当我调试到AccountController.cs文件,失败在MembershipService.ValidateUser(model.UserName,model.Password)方法。 解决方法如果您检查标准的ASP.NET MVC 3 AccountController.cs和AccountModels.cs文件,您将学习内部使用的 MembershipProvider.ValidateUser方法(通过 Membership.Provider)。如果要在web.config中存储密码,则应该使用 FormsAuthentication.Authenticate方法。例如: public class AuthorizationController : Controller
{
public ActionResult LogOn()
{
return View("LogOn");
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult LogOn(string userName,string password,bool rememberMe,string returnUrl)
{
if (!ValidateLogOn(userName,password))
return View("LogOn");
FormsAuthentication.SetAuthCookie(userName,rememberMe);
if (!string.IsNullOrEmpty(returnUrl))
return Redirect(returnUrl);
else
return RedirectToAction("Index","News");
}
private bool ValidateLogOn(string userName,string password)
{
if (string.IsNullOrEmpty(userName))
ModelState.AddModelError("username","User name required");
if (string.IsNullOrEmpty(password))
ModelState.AddModelError("password","Password required");
if (ModelState.IsValid && !FormsAuthentication.
Authenticate(userName,password))
ModelState.AddModelError("_FORM","Wrong user name or password");
return ModelState.IsValid;
}
public RedirectToRouteResult LogOff()
{
FormsAuthentication.SignOut();
return RedirectToAction("LogOn");
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在asp.net页面之间传递数据
- asp.net – 将单元测试慢慢集成到项目中的步骤
- ASP.NET中maxPageStateFieldLength的最佳值是多少?
- asp.net – 如何使我的Web应用程序无状态仍然做一些有用的事
- asp.net-core – 加密ASP.Net Core中的连接字符串和其他配置
- ASP.NET 1.1到4.0迁移:事件不工作
- asp.net-core – CoreCLR中的MD5CryptoServiceProvider的替
- asp.net-mvc – 图像优化框架未初始化
- asp.net – Windows 7 SP1 IIS错误与“无效的应用程序路径”
- asp.net – 没有透明度的div中的div的不透明度
推荐文章
站长推荐
- 如何在ASP.NET生成的Word文件中嵌入图像
- asp.net – 是果园还是Umbraco MVC?
- asp.net-mvc – ASP.NET MVC是否有分页解决方案,
- asp.net-mvc – 通用列表属性的必需属性
- ASP.NET MVC 3自定义身份验证/授权
- asp.net – 如何在IIS7中为HttpHandler注册多个路
- .net – ApiController与ODataController在露出D
- 如何在回发期间在asp.net mvc中保留部分视图模型
- 如何设置特定于ASP.NET请求的log4net上下文属性?
- asp.net – 在实体框架中使用PersianCalendar作为
热点阅读
