使用Session来存储身份验证?

时间:2010-06-03 11:38:46

标签: asp.net forms-authentication

我有很多problems with FormsAuthentication,而且我正在考虑将login存储在Session

Login:
Session["Auth.ClientId"] = clientId;

IsAuthenticated:
Session["Auth.ClientId"] != null;

Logout;
Session["Auth.ClientId"] == null;

无论如何,我并没有真正使用FormsAuthentication的大部分花里胡哨。这是一个坏主意吗?

2 个答案:

答案 0 :(得分:2)

我不会在会话中存储任何有价值的信息。

对于身份验证,我会使用:

if (HttpContext.Current.User.Identity.IsAuthenticated)
{
    // Then u use 
    // this.User.Identity.Name as my membership_id so i could call this everywhere
}else
{
    //Redirect to Login
    //gettting my LoginPageAddress
    Response.Redirect(ConfigurationSettings.AppSettings["LoginPage"]);
}

登录是这样的:

FormsAuthentication.SetAuthCookie(membership_ID, false)

无论如何希望这有帮助

答案 1 :(得分:0)

我认为这不是一个坏主意,我看到很多网站使用会话和数据库来存储身份验证数据,但是还有其他方法可以避免使用formauthentication表但仍然可以使用像角色这样的事情。

How do I create a custom membership provider for ASP.NET MVC 2?

有很好的例子。