• 视频教程:小型登陆系统(一)


    登陆界面

    查看更多精彩图片

    v    登陆使用MembershipValidateUser方法验证用户名与密码,根据用户所拥有的角色转到管理页面与个人页面,判断方法Roles.IsUserInRole(userName, “Admin”),可以使FormsAuthentication.SetAuthCookie(userName, false);方法把身份票证保存到Cookie集合,这里第二个参数使用的是false,关闭浏览器Cookie就失效。
     
     

    Login.aspx

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Data.Common;

    namespace RegeditOfAdoDotnet
    {
        public partial class Login : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //清除缓存
                Response.Buffer = true;
                Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
                Response.Expires = 0;
                Response.CacheControl = "no-cache";
                Response.AddHeader("Pragma", "No-Cache");
            }

            /// <summary>
            /// 登陆
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void btnLogin_Click(object sender, EventArgs e)
            {
                if (Page.IsValid)
                {
                    try
                    {
                        if (Membership.ValidateUser(txtUserName.Text, txtPassWord.Text))//验证提供的用户和密码是否有效的
                        {
                            if (Roles.IsUserInRole(txtUserName.Text, "Admin"))//判断当前登陆的用户是不是管理员
                            {
                                // 自己写的验证票--创建身份验证票
                                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now, DateTime.Now.AddMinutes(20), false, "Admin");
                                // 创建身份验证票的加密字符串
                                string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                                // 将加密字符串存储在HttpCookie 对象中
                                HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                                // 将此cookie 添加到返回给用户浏览器的cookie 集合中
                                Response.Cookies.Add(authCookie);
                                // 将用户重定向到最初请求的页
                                Response.Redirect("~/Admin/AdminInfo.aspx", true);
                            }
                            else
                            {
                                FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false);//将经过身份验证的用户重定向回最初请求的URL 或默认URL。
                                FormsAuthentication.SetAuthCookie(txtUserName.Text, false);//为提供的用户名创建一个身份验证票证,并将其添加到响应的Cookie 集合或URL。
                                Response.Redirect("~/Admin/GeneralUserInfo.aspx", true);
                            }
                        }
                        else
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('登陆失败,请检查用户名和密码,重新输入!');</script>");
                        }
                    }
                    catch (Exception exp)
                    {
                        //捕获异常
                        Response.Write(exp.Message); //输出错误信息
                    }
                }
            }

            /// <summary>
            /// 退出
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void btnExit_Click(object sender, EventArgs e)
            {
                Session.Clear();
                FormsAuthentication.SignOut();
                Response.Redirect("~/Goodby.aspx");
            }
        }
    }

    登陆系统演示地址

  • 相关阅读:
    存储数据的大小端模式
    双链表插入 删除详解
    php_match/preg_match_all 默认有字符串长度限制
    百度编辑器:获取编辑器的内容
    phalcon: update修改数据却变成了insert插入数据
    MySQL按照汉字的拼音排序,mysql汉字排序
    [转载]Eclipse提示No java virtual machine
    lhgdialog: iframe页面里面的,确定,关闭、取消按钮的操作
    js树目录结构
    mysql:恢复mysql表结构
  • 原文地址:https://www.cnblogs.com/Gemgin/p/3136382.html
Copyright © 2020-2023  润新知