zoukankan      html  css  js  c++  java
  • 视频教程:小型登陆系统(一)

    登陆界面

    查看更多精彩图片

    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");
            }
        }
    }

    登陆系统演示地址

  • 相关阅读:
    异步FIFO的Verilog实现
    二进制格雷码与二进制自然码
    握手协议
    电容充放电和开关电容
    Vivado自定义IP封装流程
    【转】warning 之 [IP_Flow 19-3153]
    【转】mipi-csi-2解读
    版本管理-link
    [转载]yuv和yCbCr的差异
    【转】用verilog实现RGB格式图像到YCbCr或YUV格式的转换及其验证方法 (RGB2YCrCb)(RGB2YUV)
  • 原文地址:https://www.cnblogs.com/Gemgin/p/3136382.html
Copyright © 2011-2022 走看看