.net登录的一般模式

2018-06-18 01:46:43来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

思路:

1、检查登录:

private string CheckLogin(HttpContext context)
{
    Model.TUser model = (Model.TUser)context.Session["USERModel"];
    if (model == null)
    {
        return "{\"msg\": \"0\", \"msgbox\": \"当前为退出\"}";
    }
    else
    {
        return "{\"msg\": \"1\", \"msgbox\": \"当前为登录\"}";
    }
}

2、点击登录:

private string Login(HttpContext context)
{
    BLL.TUser _blluser = new BLL.TUser();
    string loginname = context.Request.Params["loginname"];
    string password = context.Request.Params["password"];
    string issave = context.Request.Params["issave"];//是否保存密码

    Model.TUser model_user = _blluser.GetModel("UserLogin='" + loginname + "'");

    bool isSuccess = false;
    string cookiesPassword = "";
    //判断 Cookies 是否存在
    if (HttpContext.Current.Request.Cookies["password"] != null)
    {
        cookiesPassword = HttpContext.Current.Request.Cookies["password"].Value;
        if (password == cookiesPassword) /// 输入 和 Cookies 密码 一样
        {
            //登录成功
            isSuccess = true;
        }
    }

    //判断输入的密码和数据库的是否匹配
    if (!isSuccess)
    {
        if (password == model_user.PassWord)
        {
            //登录成功
            isSuccess = true;
        }
        else
        {
            //登录失败
            isSuccess = false;
        }
    }

    if (isSuccess)
    {
        context.Session["USERModel"] = model_user;
    }
    else
    {
        //登录失败 密码不对
        return "{\"msg\": \"0\", \"msgbox\": \"密码错误\"}";
    }

    //要保存,则写入cookie
    if (issave == "true")
    {
        HttpCookie cookieLogin = new HttpCookie("loginname");
        cookieLogin.Expires = DateTime.Now.AddDays(10);
        cookieLogin.Value = model_user.UserLogin;
        HttpContext.Current.Response.Cookies.Add(cookieLogin);

        HttpCookie cookiePassword = new HttpCookie("password");
        cookiePassword.Expires = DateTime.Now.AddDays(10);
        cookiePassword.Value = password;
        HttpContext.Current.Response.Cookies.Add(cookiePassword);
    }
    else
    {//设置cookie过期,删除cookie
        HttpCookie cookieLogin = new HttpCookie("loginname");
        cookieLogin.Expires = DateTime.Now.AddDays(-1);
        HttpContext.Current.Response.Cookies.Add(cookieLogin);

        HttpCookie cookiePassword = new HttpCookie("password");
        cookiePassword.Expires = DateTime.Now.AddDays(-1);
        HttpContext.Current.Response.Cookies.Add(cookiePassword);
    }

    return "{\"msg\": \"1\", \"msgbox\": \"登录成功\"}";
}

3、页面刷新时获取用户保存的账户名和密码并免登陆

private string IsSaveUserLogin(HttpContext context)
{
    ///判断 Cookies 是否存在
    if (HttpContext.Current.Request.Cookies["loginname"] == null || HttpContext.Current.Request.Cookies["password"] == null)
    {
        return "{\"msg\":-1, \"msgbox\": \"帐号没保存\", \"loginname\": \"\", \"password\": \"\"}";
    }
    else
    {
        string loginname = HttpContext.Current.Request.Cookies["loginname"].Value;
        string password = HttpContext.Current.Request.Cookies["password"].Value;
        //免登陆
        CookieLogin(context, loginname, password);
        return "{\"msg\":1, \"msgbox\": \"帐号保存过\", \"loginname\": \"" + loginname + "\", \"password\": \"" + password + "\"}";
    }
}

private void CookieLogin(HttpContext context, string cookiesLoginname, string cookiesPassword)
{
    Model.TUser model_user = new Model.TUser();
    BLL.TUser _blluser = new BLL.TUser();
    bool isSuccess = false;
    ///判断 Cookies 是否存在
    if (!string.IsNullOrEmpty(cookiesLoginname) && !string.IsNullOrEmpty(cookiesPassword))
    {
        model_user = _blluser.GetModel("UserLogin='" + cookiesLoginname + "'");
        if (model_user.PassWord == cookiesPassword) /// 数据库 和 Cookies密码 一样
        {
            //登录成功
            isSuccess = true;
        }
    }

    if (isSuccess)
    {
        //登录成功
        context.Session["USERModel"] = model_user;
    }
    else
    {
        //登录失败 密码不对
        //return "{\"msg\": \"0\", \"msgbox\": \"密码错误\"}";
    }
}

4、用户退出-清除cookie和session

private string UserExit(HttpContext context)
{
    //清除cookie
    //HttpContext.Current.Request.Cookies.Clear();//无效
    HttpCookie cookieLogin = new HttpCookie("loginname");
    cookieLogin.Expires = DateTime.Now.AddDays(-1);
    HttpContext.Current.Response.Cookies.Add(cookieLogin);

    HttpCookie cookiePassword = new HttpCookie("password");
    cookiePassword.Expires = DateTime.Now.AddDays(-1);
    HttpContext.Current.Response.Cookies.Add(cookiePassword);

    context.Session["USERModel"] = null;
    context.Session.Clear();
    return "";
}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:NLog路由规则和上下文信息

下一篇:Xml学习笔记(1)