zoukankan      html  css  js  c++  java
  • ASP.net中Security.FormsAuthentication验证用户的状态(匿名|已登录)

    专题图ylbtech-asp.net-logo编号:ylbtechASPnet100010010Security

    1,功能描述

       ASP.net下利用System.Web.Security.FormsAuthentication类,验证用户的状态(匿名|已登录)

    以项目为例1,在网站根节点下/UserInfo.aspx不允许匿名访问,其它都允许访问。2,在后(/Admin/)下/Admin/AdminLogin.aspx允许匿名访问,其它都拒绝访问。

    2,技术与环境

      ASP.net下System.Web.Security.FormsAuthentication类,验证用户的状态(匿名|已登录)

    3,数据库设计

     无

    4,功能截图

     4.1,前台

      匿名状态下

     4.1.1  /Default.aspx

    4.1.2  访问/UserInfo页面,将拒绝,并跳转到/SignIn页面

                     

    4.2,后台

       已登录状态下 /Admin/Default.aspx页面

     

    5,代码分析

     5.1,前台

       5.1.1  /web.config   配置 

    <?xml version="1.0"?>
    <!--
      有关如何配置 ASP.NET 应用程序的详细信息,请访问
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    <configuration>
        <system.web>
            <!--
                通过 <authentication> 节可以配置 ASP.NET 用来 
                识别进入用户的
                安全身份验证模式。 
            -->
            <authentication mode="Forms">
                <forms loginUrl="SignIn.aspx" protection="All"></forms>
            </authentication>
        
        
            <compilation debug="true" targetFramework="4.0"/>
        </system.web>
      <!--
        配置根节点下面的页面
      -->
      <location path="UserInfo.aspx">
        <system.web>
          <authorization>
            <deny users="?"/>
          </authorization>
        </system.web>
      </location>
      
    </configuration>

       5.1.2 /SignIn.aspx.cs  用户登录验证代码

    using System;
    
    using System.Web.Security;
    public partial class Signin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
    
            //为用户创建一个身份验证票证
            FormsAuthentication.SetAuthCookie("ylb", false);
            
            //跳转到上一个页面
            string continueUrl = Request.QueryString["ReturnUrl"];
            if (String.IsNullOrEmpty(continueUrl))
            {
                continueUrl = "~/";
            }
            Response.Redirect(continueUrl);
        }
    }

       5.1.3  /Admin/web.config

    <?xml version="1.0"?>
    <configuration>
      <!--
        允许匿名用户访问"/Admin/AdminLogin"页面
      -->
      <location path="AdminLogin.aspx">
        <system.web>
          <authorization>
            <allow users="*"/>
          </authorization>
        </system.web>
      </location>
      <!--
        拒绝匿名用户访问“/Admin/”下的页面,除去那些允许的。
      -->
        <system.web>
          <authorization>
            <deny users="?"/>
          </authorization>
        </system.web>
    </configuration>

    5.2,后台

      5.2.1  /Admin/Default.aspx.cs  用户退出代码

    using System;
    
    using System.Web.Security;
    public partial class Admin_Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void btnLogout_Click(object sender, EventArgs e)
        {
            //删除Forms身份验证票
            FormsAuthentication.SignOut();
    
            //跳转到首页
            Response.Redirect("~/Default.aspx");
        }
    }
    6,示例|讲解案例下载

    博客园讲解: http://ylbtech.cnblogs.com/

    百度文库开发文档: http://passport.baidu.com/?business&aid=6&un=ylbtech#7

    谷歌开源代码下载: http://code.google.com/p/ylbtechaspnet/downloads/list

    请单击“ylbtechASPnet100010010Security”

     

    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

     

    最终目标

     代码的国际化标准示例 ylb,tech”,最大程度地规范软件编程开发统一,优质, 高效,易学,为建设软件强国(中国)而努力。

     

  • 相关阅读:
    在Flex or AIR中检测你的网络连接是否正常
    设置Adobe Air应用程序属性
    airMeeting
    用Adobe Flex3开发AIR应用程序–入门指南
    Adobe Flash Player 9.0.124 的安全策略更改
    分发,安装和运行AIR应用程序
    永远置于顶层(always on top)的AIR应用
    翻译:SWFObject 2.0官方文档
    C#温故而知新学习系列之.NET框架高级特性—概述.NET框架中的反射(一)
    C#温故而知新学习系列之字符串处理—指定字符串的显示格式(一)
  • 原文地址:https://www.cnblogs.com/ylbtech/p/2652090.html
Copyright © 2011-2022 走看看