带登陆功能的网站是什么原理
刚学做一个有登录功能的网站,就好像是网上书店差不多,用户登陆后可以订书,加入订单和购买等,用户没登陆就只能看,不可以进行上面的操作,由于对这种网站不了解,我先问这几个没有什么水准的问题啊:
1、这种网站的原理是什么?
2、怎么判断用户是不是登陆了,用户登陆后要怎么保持一个登陆的状态,网上找了一下说用Cookies,但是我不太会用,也不了解。
3、数据库设计有什么不一样的地方吗?
4、我是用 Linq来做的。
5、最好能给一些小例子看看。
6、谢谢各位啊。 登陆 cookies linq
[解决办法]
using System;
using System.Web;
using Model;
/// <summary>
///LoginUser 的摘要说明
/// </summary>
public class LoginUser
{
private HttpCookie Cookie;
// 登录
public static void Login(LoginInfo user)
{
LoginUser login = new LoginUser();
login.WriteCookie("Ihdy", user.UserId);
login.WriteCookie("Ymxe", user.UserName);
login.WriteCookie("Dmdy", user.Passd);
login.WriteCookie("Xgng", user.Name);
}
// 退出
public static void Logout()
{
LoginUser login = new LoginUser();
login.CookieRemoves("Ihdy");
login.CookieRemoves("Ymxe");
login.CookieRemoves("Dmdy");
login.CookieRemoves("Xgng");
}
/// <summary>
/// 添加Cookie
/// </summary>
/// <param name="cookieName"></param>
/// <param name="obj"></param>
private void WriteCookie(string cookieName, object obj, bool Ext = true)
{
if (obj != null)
{
Cookie = new HttpCookie(cookieName);
Cookie.Value = Ext ? HttpUtility.UrlEncode(obj.ToString().Ext_GetEncrypt()) : obj.ToString();
HttpContext.Current.Response.Cookies.Add(Cookie);
}
}
/// <summary>
/// 清除cookie
/// </summary>
/// <param name="cookeName"></param>
private void CookieRemoves(string cookeName)
{
HttpContext.Current.Response.Cookies.Remove(cookeName);
Cookie = new HttpCookie(cookeName);
Cookie.Expires = DateTime.Now.AddDays(-100);
HttpContext.Current.Response.Cookies.Add(Cookie);
}
/// <summary>
/// 用户ID
/// </summary>
public static int UserId
{
get
{
if (HttpContext.Current.Request.Cookies["Ihdy"] != null)
{
return int.Parse(HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["Ihdy"].Value).Ext_GetDecrypt());
}
else
{
return 0;
}
}
}
/// <summary>
/// 用户名
/// </summary>
public static string UserName
{
get
{
if (HttpContext.Current.Request.Cookies["Ymxe"] != null)
{
return HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["Ymxe"].Value).Ext_GetDecrypt();
}
else
{
return string.Empty;
}
}
}
/// <summary>
/// 用户密码
/// </summary>
public static string Passd
{
get
{
if (HttpContext.Current.Request.Cookies["Dmdy"] != null)
{
return HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["Dmdy"].Value).Ext_GetDecrypt();
}
else
{
return string.Empty;
}
}
}
/// <summary>
/// 用户姓名
/// </summary>
public static string Name
{
get
{
if (HttpContext.Current.Request.Cookies["Xgng"] != null)
{
return HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["Xgng"].Value).Ext_GetDecrypt();
}
else
{
return string.Empty;
}
}
}
}