ASP.NET MVC Form身份验证

zhang_13245768 2011-09-28 04:37:50
最近在学习MVC中实现Form身份验证的时候,出现了一个问题,在控制器中添加了用于Form验证的接口和实现方法

/// <summary>
/// 定义Form验证的接口
/// </summary>
public interface IFormsAuthenticationService
{
void SignIn(string userName, bool createPersistentCookie);
void SignOut();
}

/// <summary>
/// Form验证的实现
/// </summary>
public class FormsAuthenticationService : IFormsAuthenticationService
{
public void SignIn(string userName, bool createPersistentCookie)
{
if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");

FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
}

public void SignOut()
{
FormsAuthentication.SignOut();
}
}


public class BoperatorController : Controller
{
public IFormsAuthenticationService FormsService { get; set; }

protected override void Initialize(RequestContext requestContext)
{
if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
base.Initialize(requestContext);
}
/// <summary>
/// 验证用户信息
/// </summary>
/// <param name="code">用户名</param>
/// <param name="newpass">密码</param>
/// <returns></returns>
public bool ValidateUser(string code, string newpass)
{
if (String.IsNullOrEmpty(code)) throw new ArgumentException("Value cannot be null or empty.", "userName");
if (String.IsNullOrEmpty(newpass)) throw new ArgumentException("Value cannot be null or empty.", "password");
var boperator = boperatorService.GetBoperator(code, newpass);
if (boperator != null)
return true;
else
return false;
}


public ActionResult LogOn()
{
return View(new LogOnModel());
}

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (ValidateUser(model.code, model.newpass))
{
FormsService.SignIn(model.code, model.RememberMe);
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Account");
}
}
else
{
ModelState.AddModelError("", "用户名或密码错误!");
}
}
return View(model);
}

public ActionResult LogOff()
{
FormsService.SignOut();
return RedirectToAction("LogOn", "Boperator");
}
}

然后在Global中添加了构造函数和事件

public MvcApplication()
{
AuthorizeRequest += new EventHandler(MvcApplication_AuthorizeRequest);
}
void MvcApplication_AuthorizeRequest(object sender, EventArgs e)
{
IIdentity id = Context.User.Identity;
if (id.IsAuthenticated)
{
string[] roles = new string[1];
roles[0] = id.Name;
Context.User = new GenericPrincipal(id, roles);
}
}


现在在其他控制器的方法中加入了特性

[Authorize]
/// <summary>
/// 显示所有账户信息
/// </summary>
/// <returns>账户信息列表</returns>
public ActionResult Index()


我在登录的时候输入用户名和密码后,
...全文
1045 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
MSDNXGH 2011-09-29
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zhang_13245768 的回复:]
我估计也是缓存的问题,那要在什么时候去清除页面的缓存呢?
[/Quote]
怎么可能是缓存问题,你要想试一下是不是缓存也很容易,缓存一般在本地,你在该页面再加一个链接,进去后,点该链接,如果链接能链接通,说明,不是缓存,是确实,该问到该页。也或者,你添加一个获取时间,如果是缓存,那么时间也是之前的时间,但如果时间显示是现在的,就不可能是缓存。

什么情况下,能确定是缓存昵?就是用JS后退,然后,浏览器后退,才会出现缓存,并且缓存在是本地计算机,你没法删除,JS也操作不了。
zhang_13245768 2011-09-29
  • 打赏
  • 举报
回复
我估计也是缓存的问题,那要在什么时候去清除页面的缓存呢?
萤火架构 2011-09-29
  • 打赏
  • 举报
回复
感觉还是缓存问题吧。
MSDNXGH 2011-09-29
  • 打赏
  • 举报
回复
当然不能进,开什么玩笑!
zhang_13245768 2011-09-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 webdiyer 的回复:]
是不是浏览器缓存的原因?注销后再进入首页时刷新一下试试
[/Quote]
我试过了,注销后刷新还是可以进去。
webdiyer 2011-09-29
  • 打赏
  • 举报
回复
是不是浏览器缓存的原因?注销后再进入首页时刷新一下试试
zhang_13245768 2011-09-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 msdnxgh 的回复:]
MVC FORM验证最简方式,


C# code


web.confing中加入
<authentication mode="Forms">
<forms loginUrl="~/Home/index" timeout="2880" />
</authentication>



通过验过验证

C# code


……
[/Quote]
我们的方式差不多,你有试过注销后在浏览器中输入地址能否进入吗?
MSDNXGH 2011-09-29
  • 打赏
  • 举报
回复
MVC FORM验证最简方式,


web.confing中加入
<authentication mode="Forms">
<forms loginUrl="~/Home/index" timeout="2880" />
</authentication>


通过验过验证

if (new account.Data.Bll().Login(username, possWord))//验证身份
{
FormsAuthentication.SetAuthCookie(username, false);//验证通过
return RedirectToAction("index", "bc");
}


受控制验证页

在上面加入[Authorize]

正如你所写的

[Authorize]
/// <summary>
/// 显示所有账户信息
/// </summary>
/// <returns>账户信息列表</returns>
public ActionResult Index()
MSDNXGH 2011-09-29
  • 打赏
  • 举报
回复
加断点,查看一下,验证过程,反正,你也写了这么多代码
zhang_13245768 2011-09-29
  • 打赏
  • 举报
回复
来个人帮忙看看
zhang_13245768 2011-09-29
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 duoduo518 的回复:]
“FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);”有这句,就是把认证信息写入Cookie了,要查看你的LogOut通过FormService(没看到定义)是否真正执行了FormsAuthentication.SignOut()
[/Quote]
public class BoperatorController : Controller
{
public IFormsAuthenticationService FormsService { get; set; } protected override void Initialize(RequestContext requestContext)
{
if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
base.Initialize(requestContext);
}
这里定义的
duoduo518 2011-09-29
  • 打赏
  • 举报
回复
“FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);”有这句,就是把认证信息写入Cookie了,要查看你的LogOut通过FormService(没看到定义)是否真正执行了FormsAuthentication.SignOut()
萤火架构 2011-09-29
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 zhang_13245768 的回复:]

引用 11 楼 msdnxgh 的回复:
引用 10 楼 zhang_13245768 的回复:
我估计也是缓存的问题,那要在什么时候去清除页面的缓存呢?

怎么可能是缓存问题,你要想试一下是不是缓存也很容易,缓存一般在本地,你在该页面再加一个链接,进去后,点该链接,如果链接能链接通,说明,不是缓存,是确实,该问到该页。也或者,你添加一个获取时间,如果是缓存,那么时间也是之前的时间,但如……
[/Quote]
你这就是缓存的问题啊,刷新就可以,重新打开就可以。
萤火架构 2011-09-29
  • 打赏
  • 举报
回复
你这个程序我测试过了,没有你提到的问题。
zhang_13245768 2011-09-29
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 msdnxgh 的回复:]
引用 10 楼 zhang_13245768 的回复:
我估计也是缓存的问题,那要在什么时候去清除页面的缓存呢?

怎么可能是缓存问题,你要想试一下是不是缓存也很容易,缓存一般在本地,你在该页面再加一个链接,进去后,点该链接,如果链接能链接通,说明,不是缓存,是确实,该问到该页。也或者,你添加一个获取时间,如果是缓存,那么时间也是之前的时间,但如果时间显示是现在的,就不可能是缓存。

什……
[/Quote]
我在登陆页面中加入了链接,注销操作结束后,有两种情况
1.如果在浏览器中输入地址,是可以进去的,这个时候刷新页面,会回到登陆页面;
2.点击链接是进不去的。
糊涂中啊
zhang_13245768 2011-09-28
  • 打赏
  • 举报
回复
我在登录的时候输入用户名和密码后,调用的是
public ActionResult LogOn(LogOnModel model, string returnUrl)
OK进去到/Account/index,没问题,然后我点击注销,调用的是
public ActionResult LogOff()
目前是没什么问题的,可是当我在浏览器中再次输入/Account/index,因为我已经注销过了,
按道理来说是进不去的,可是也进去了,求大神指点错误出在哪里,代码有点多,请谅解。

62,042

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧