zoukankan      html  css  js  c++  java
  • asp.net 使用内置票据来判断某用户是否有权限登录此网页

            网页中通常会用到登录用户名和密码,才能进行一系列的增删改查操作,这时我们可以

            使用Session来进行页面之间传值。判断Session的值是不是为空和Session值来区分

            有没有权限,但是这种方法有很大的缺点,一是Session是有时间限制的,二是我们每

            一个页面都需要进行判断,影响了编程效率。这时我们可以使用asp.net内置票据认证,

            他的作用是:根据你的设置,在进入到某一个目录下的页面时自动判断你是否有权限访问

            这个页面,没有权限则自动跳转到你预先设置的登录页。说了那么多,让我们看看asp.net

            内置票据认证的用法:

            已一个项目为例:此项目中有个admin文件夹,此文件夹下保存的是网站后台的页面文件,

            这个文件夹下的页面必须在login.aspx页面中登录用户名和密码后才可以访问,步骤如下:

            第一步:在根目录建立一个全局应用程序类(Global.asax)文件

            第二步:在Global.asax中修改Application_AuthenticateRequest如下:

            protected void Application_AuthenticateRequest(object SENDER, EventArgs e)

              {

                 if (HttpContext.Current.User != null)

                   {

                      if (HttpContext.Current.User.Identity.IsAuthenticated)

                        {

                          if (HttpContext.Current.User.Identity is FormsIdentity)

                            {

                             FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;

                             FormsAuthenticationTicket tiecket = id.Ticket;

                             string userData = tiecket.UserData;

                             string[] roles = userData.Split(',');

                             HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);

                           }

                        }

                     }

                 }

               第三步:在web.config 文件中配置目录权限及登录页(在system.web节点中) 

              <authentication mode="Forms">

                   <forms name="mycook" loginUrl="admin/login.aspx" protection="All" path="/"/>

              </authentication>

              第四步:在system.web节点外面配置权限

              <location path="admin">      //在admin文件夹下

                     <system.web>

                           <authorization>

                                <allow roles="admin"/> //允许admin

                                <deny users="*"/>      //拒绝所有用户

                            </authorization>

                      </system.web>

              </location>

             <location path="admin/login.aspx"> //在admin/login.aspx页面

                   <system.web>

                       <authorization>

                           <allow users="*"/>  //这个页面允许所有人登录

                       </authorization>

                    </system.web>

             </location>

            第五步:在登录页添加代码

              1.在登录页面添加引用

              using System.Web.Security;

               2.在登录页的登录事件中的登录成功后添加如下代码

                HttpCookie cook;

                string strReturnURL;   //登录成功后返回的url

                string roles="admin";  //用户角色

                //下面代码的意思是传入用户名name,将此用户角色变为admin使其有权限

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(

                    1, name, DateTime.Now, DateTime.Now.AddMinutes(30), false, roles);

                cook = new HttpCookie("mycook");

                cook.Value = FormsAuthentication.Encrypt(ticket);

                Response.Cookies.Add(cook);

                strReturnURL = Request.Params["ReturnUrl"];

                if (strReturnURL != null)

                {

                    Response.Redirect(strReturnURL);

                }

                else

                {

                    Response.Redirect("login.aspx");//没有权限跳回页面

                }

         

     

          

  • 相关阅读:
    增加正则项Regularization to Prevent Overfitting
    feature_column、fc.input_layer以及各种类型的column如何转化
    input_fn如何读取数据
    matplotlib.pyplot如何绘制多张子图
    机器学习之杂乱笔记
    Adobe Flash Player
    LSTM/GRU-讲得非常细致
    anaconda python36 tensorflow virtualenv
    畅通工程-HZNU寒假集训
    食物链-HZUN寒假集训
  • 原文地址:https://www.cnblogs.com/gaopin/p/2662026.html
Copyright © 2011-2022 走看看