//原来字写错了,太汗了,

做为开发者,我们希望经过我们手中出来的代码,是效率最高的,速度最快的,性能最优化的。

我们在探索代码的完美,我们在想着,是不是有更好的代码比我现在这样的写好,

我们想寻找最好的代码,

我们在想,

我们还是在想,却仍没有去敲键盘。

不知不觉浪费了好多的时间,我的还是在想,还是没有代码的呈现。

究竟什么才是最完美的代码呢?

应该是没有最完美的代码。我们在经过简单的思考后,应该执着着去写,经过时间的沉淀,不断的修改和完善,

看到别人的代码,想下人家的思考,如果优于自己,可以取其精华,结合自己的代码不断的去成长,去提高 ,

//修改1 ,已经把代码粘到代码编辑器中了,

//我认为这种代码也比较好,但是有点不想接受,逻辑上有点不顺,感觉,想寻找更好的代码和逻辑

我们往往不太注重逻辑,我们往往下笔没有神,因为copy,del的太简单,在不断的keyword,del的时候,最仍然没有出一个好的逻辑 ,

一次去面试,最后一道是算法题,我是拿着笔就开写了,写着写着,觉得要变动,没有Del,只有划,最后的结果呢,

面试纸上划下了一大片,却仍然没有一个真正的逻辑出来。

为什么不能先想好,才下笔呢。

程序不是作文,写一半就会有一半的分数,

程序不是作文,结果不对就是错的。

最近买了李天平的那本书,附上一个书中的登陆代码。这个登陆主要是用到了微软的安全类,

我不知道大家平时用的登陆,是怎么实现的。有没有比这个还优秀一点的。

比较难以理解的名字,做了一下通俗的解释。希望对大家有一些帮助。

1.负责人(principal)-运行环境  身份(identity)-用户  授权(Authentication)
   Authorization 授权 判断用户是否有权操作,比如登录的用户有没有权限访问资源或者数据库
Authentication 认证 用户的Identity. 主要有:HTTP基础认证、证书、Kerberos、Passport、NTLM、Forms-based、Digest
一般应用先authenticate用户, 判断用户是否能链接到系统。然后authorization, 判断对某个功能是否有权限。
2.HttpModuler 监视器 过滤传到httpHand上的数据

代码

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->一,

using System;
using System.Collections;
using System.Security;
using System.Security.Cryptography;
using System.Text;

namespace Permission.WebAdmin
{
    /// <summary>
    /// 用户对象的安全上下文信息
    /// </summary>
    public class AccountsPrincipal : System.Security.Principal.IPrincipal
    {

#region 属性
        protected System.Security.Principal.IIdentity identity;
        protected ArrayList permissionList;
        protected ArrayList permissionListid;
        protected ArrayList roleList;

/// <summary>
        /// 当前用户的所有角色
        /// </summary>
        public ArrayList Roles
        {
            get
            {
                return roleList;
            }
        }
        /// <summary>
        /// 当前用户拥有的权限列表
        /// </summary>
        public ArrayList Permissions
        {
            get
            {
                return permissionList;
            }
        }
        /// <summary>
        /// 当前用户拥有的权限ID列表
        /// </summary>
        public ArrayList PermissionsID
        {
            get
            {
                return permissionListid;
            }
        }
        // IPrincipal Interface Requirements:
        /// <summary>
        /// 当前用户的标识对象
        /// </summary>
        public System.Security.Principal.IIdentity Identity
        {
            get
            {
                return identity;
            }
            set
            {
                identity = value;
            }
        }
        #endregion

/// <summary>
        /// 根据用户编号构造
        /// </summary>
        public AccountsPrincipal(int userID)
        {
            identity = new SiteIdentity(userID);
            permissionList = AccountsPrincipalDLL.GetEffectivePermissionList(userID);
            permissionListid = AccountsPrincipalDLL.GetEffectivePermissionListID(userID);
            roleList = AccountsPrincipalDLL.GetUserRoles(userID);
        }
        /// <summary>
        /// 根据用户名构造
        /// </summary>
        public AccountsPrincipal(string userName)
        {
            identity = new SiteIdentity(userName);
            permissionList = AccountsPrincipalDLL.GetEffectivePermissionList(((SiteIdentity)identity).UserID);
            permissionListid = AccountsPrincipalDLL.GetEffectivePermissionListID(((SiteIdentity)identity).UserID);
            roleList = AccountsPrincipalDLL.GetUserRoles(((SiteIdentity)identity).UserID);
        }
        /// <summary>
        /// 当前用户是否属于指定名称的角色
        /// </summary>
        public bool IsInRole(string role)
        {
            return roleList.Contains(role);
        }
        /// <summary>
        /// 当前用户是否拥有指定名称的权限
        /// </summary>
        public bool HasPermission(string permission)
        {
            return permissionList.Contains(permission);
        }
        /// <summary>
        /// 当前用户是否拥有指定的权限
        /// </summary>
        public bool HasPermissionID(int permissionid)
        {
            return permissionListid.Contains(permissionid);
        }
        /// <summary>
        /// 验证登录信息
        /// </summary>
        public static AccountsPrincipal ValidateLogin(string userName, string password)
        {
            int newID;
            byte[] cryptPassword = EncryptPassword(password);
            Data.User dataUser = new Data.User();
            if ((newID = dataUser.ValidateLogin(userName, cryptPassword)) > 0)
                return new AccountsPrincipal(newID);
            else
                return null;
        }
        /// <summary>
        /// 密码加密
        /// </summary>
        public static byte[] EncryptPassword(string password)
        {
            UnicodeEncoding encoding = new UnicodeEncoding();
            byte[] hashBytes = encoding.GetBytes(password);
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] cryptPassword = sha1.ComputeHash(hashBytes);
            return cryptPassword;
        }

}
}

using System;
using System.Data;
using System.Configuration;
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 Permission.Common;
namespace Permission.WebAdmin
{
    /// <summary>
    /// 页面基类
    /// </summary>
    public class PageBase : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new System.EventHandler(this.Page_Load);
            this.Error += new System.EventHandler(this.Page_Error);
        }

#region 权限检查
        /// <summary>
        /// 页面访问权限ID。
        /// </summary>
        public virtual int PermissionID
        {
            get { return -1; }
        }

public AccountsPrincipal CurrentPrincipal
        {
            get
            {
                if (Context.User.Identity.IsAuthenticated)
                {
                    AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
                    return user;
                }
                return null;
            }
        }
        /// <summary>
        /// 当前用户信息
        /// </summary>
        public Tb_Accounts_Users CurrentUser
        {
            get
            {
                if (CurrentPrincipal == null)
                {
                    return null;
                }
                if (Session["UserInfo"] == null)
                {
                    LTP.Accounts.Bus.User currentUser = new LTP.Accounts.Bus.User(CurrentPrincipal);
                    Session["UserInfo"] = currentUser;
                }
                return Session["UserInfo"] as Tb_Accounts_Users;
            }
        }
        #endregion

#region 页面事件
        private void Page_Load(object sender, System.EventArgs e)
        {
            //网站域名或虚拟目录
            string virtualPath = ConfigurationManager.AppSettings.Get("VirtualPath");
            //登录页地址
            string loginPage = ConfigurationManager.AppSettings.Get("LoginPage");
            if (Context.User.Identity.IsAuthenticated)
            {
                AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
                if ((PermissionID != -1) && (!user.HasPermissionID(PermissionID)))
                {
                    Response.Clear();
                    Response.Write("<script defer>window.alert('您没有权限进入本页!');history.back();</script>");
                    Response.End();
                }
            }
            else
            {
                FormsAuthentication.SignOut();
                Session.Clear();
                Session.Abandon();
                Response.Clear();
                Response.Write("<script defer>window.alert('您没有权限进入本页或当前登录用户已过期!http://www.cnblogs.com/hsapphire/admin/file://n/请重新登录或与管理员联系!');parent.location='" + virtualPath + "/" + loginPage + "';</script>");
                Response.End();
            }
        }

protected void Page_Error(object sender, System.EventArgs e)
        {
            string errMsg = "";
            Exception currentError = Server.GetLastError();
            errMsg += "系统发生错误:<br/>" +
                "错误地址: " + Request.Url.ToString() + "<br/>" +
                "错误信息: " + currentError.Message.ToString() + "<br/>";
            Response.Write(errMsg);
            Server.ClearError();//要注意这句代码的使用,清除异常。

}
        #endregion

#region URL参数
        public virtual string Name
        {
            get
            {
                if ((Request["name"] != null) && (Request["name"].ToString() != ""))
                {
                    return Request.QueryString["name"].Trim();
                }
                return "";
            }
        }
        #endregion
    }

}

三。

using System;
using System.Collections.Generic;
using System.Text;

using Permission.Common;
using System.Security.Cryptography;
namespace Permission.WebAdmin
{
    /// <summary>
    /// 当前用户的标识对象
    /// </summary>
    [Serializable]
    public class SiteIdentity : System.Security.Principal.IIdentity
    {
        #region  用户属性
        private string userName;
        private string trueName;
        private string email;
        private byte[] password;
        private int userID;
        private string sex;

/// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get
            {
                return userName;
            }
        }
        /// <summary>
        /// 真实姓名
        /// </summary>
        public string TrueName
        {
            get
            {
                return trueName;
            }
        }
        /// <summary>
        /// 邮箱
        /// </summary>
        public string Email
        {
            get
            {
                return email;
            }
        }
        /// <summary>
        /// 用户编号
        /// </summary>
        public int UserID
        {
            get
            {
                return userID;
            }
        }

/// <summary>
        /// 密码
        /// </summary>
        public byte[] Password
        {
            get
            {
                return password;
            }
        }
        /// <summary>
        /// 性别
        /// </summary>
        public string Sex
        {
            get
            {
                return sex;
            }
        }
        #endregion

#region IIdentity interface requirments:

/// <summary>
        /// 当前用户的名称
        /// </summary>
        public string Name
        {
            get
            {
                return userName;
            }
        }

/// <summary>
        /// 获取所使用的身份验证的类型。
        /// </summary>
        public string AuthenticationType
        {
            get
            {
                return "Custom Authentication";
            }
            set
            {
                // do nothing
            }
        }
        /// <summary>
        /// 是否验证了用户
        /// </summary>
        public bool IsAuthenticated
        {
            get
            {
                return true;
            }
        }
        #endregion

/// <summary>
        /// 根据用户名构造
        /// </summary>
        public SiteIdentity(string currentUserName)
        {
            Tb_Accounts_Users entityUser=BllAccess . UserDLL.UserGetModelByUserName(currentUserName);
             userName = currentUserName;
             trueName = entityUser.TrueName;
             email = entityUser.Email;
             userID = entityUser.UserID;
             password = entityUser.Password;
             sex = entityUser.Sex;
        }
        /// <summary>
        /// 根据用户ID构造
        /// </summary>
        public SiteIdentity(int currentUserID)
        {
            Tb_Accounts_Users entityUser = UserDLL.UserGetModelByUserID(currentUserID);
            userName = entityUser.UserName;
            trueName = entityUser.TrueName;
            email = entityUser.Email;
            userID = currentUserID;
            password = entityUser.Password;
            sex = entityUser.Sex;
        }
        /// <summary>
        /// 检查当前用户对象密码
        /// </summary>
        public int TestPassword(string password)
        {
            // At some point, we may have a more complex way of encrypting or storing the passwords
            // so by supplying this procedure, we can simply replace its contents to move password
            // comparison to the database (as we've done below) or somewhere else (e.g. another
            // web service, etc).
            UnicodeEncoding encoding = new UnicodeEncoding();
            byte[] hashBytes = encoding.GetBytes(password);
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] cryptPassword = sha1.ComputeHash(hashBytes);
            return UserDLL.TestPassword(userID, cryptPassword);
        }

}
}

登录代码,程序不是作文相关推荐

  1. springboot微信信小程序授权登录代码

    以下是 Spring Boot 微信小程序授权登录代码的一个简单示例: @RestController public class LoginController {@Autowiredprivate ...

  2. “附近的小程序”可以直接找“餐饮” 非管理员也能登录小程序了

    "附近的小程序"能力升级,用户可以更快找到特定场景下的小程序:小程序管理后台成员管理的功能升级,非管理员也能登录小程序了. "附近的小程序"能力升级 " ...

  3. python运行不了程序代码_python怎么运行代码程序

    展开全部 一.使用Python的解释器: 1.安装python一般都会有一个交互式32313133353236313431303231363533e78988e69d8331333433653964解 ...

  4. Spring Security第1部分–具有数据库的简单登录应用程序

    什么是Spring Security? Spring Security是一个提供安全解决方案的框架,可在Web请求级别和方法级别上处理身份验证和授权. Spring安全性通过两种方式处理安全性. 一种 ...

  5. HTML5七夕情人节表白网页(抖音-流动爱心表白)HTML+CSS+JavaScript 求婚示爱代码 520情人节告白代码 程序员表白源码 3D旋转相册 js烟花代码 爱心表白网页

    HTML5七夕情人节表白网页❤抖音-流动爱心表白❤ HTML+CSS+JavaScript 求婚示爱代码 520情人节告白代码 程序员表白源码 3D旋转相册 js烟花代码 爱心表白网页 这是程序员表白 ...

  6. php模拟QQ登录获得skey码,php模拟qq登录代码

    php模拟qq登录代码 本文讲述了php如何模拟qq登录,原理是用curl模拟发送post登录,cookie保存本地,这里代码理论可以支持永久单挂qq,下面就让我们来看看吧.<?php $qqn ...

  7. HTML5七夕情人节表白网页(流星动画3D相册) HTML+CSS+JS 求婚 html生日快乐祝福代码网页 520情人节告白代码 程序员表白源码 3D旋转相册 js烟花代码 css爱心表白

    HTML5七夕情人节表白网页❤流星动画3D相册❤ HTML+CSS+JS 求婚 html生日快乐祝福代码网页 520情人节告白代码 程序员表白源码 抖音3D旋转相册 js烟花代码 css爱心表白 这是 ...

  8. 网站QQ扫码登录代码及详细说明

    简介: 您应该已经看到所有扫描代码的网站,以登录,获取用户信息或验证QQ号 今天,我将为您详细介绍QQ二维码扫码和登录代码. 快速将QQ登录名添加到您的网站 每个人都可以通过扫描代码,获取cookie ...

  9. HTML5七夕情人节表白网页(星空萤火虫) HTML+CSS+JS 求婚 html生日快乐祝福代码网页 520情人节告白代码 程序员表白源码 抖音3D旋转相册 js烟花代码 css爱心表白

    HTML5七夕情人节表白网页(星空萤火虫) HTML+CSS+JS 求婚 html生日快乐祝福代码网页 520情人节告白代码 程序员表白源码 抖音3D旋转相册 js烟花代码 css爱心表白 这是程序员 ...

最新文章

  1. 服务器负载均衡(1)
  2. 【转】Node.js最新Web技术栈(2015年5月)
  3. 负数比较大小_【教研活动】整体把握负数脉络 深度解读教材意图——鲤城区实验小学数学组单元整体教学系列研讨活动...
  4. 当你没有能力的时候,所有的友善都是廉价的
  5. Linux编译LLVM,如何使用ninja快速编译LLVM和Clang(以llvm3.3为例子)
  6. python安装教程-Python安装包+安装教程
  7. 【vijos】P1190 繁忙的都市
  8. SciTE AMPL配置问题
  9. ios沙箱软件_ios沙盒2存档-ios沙盒2最新版下载0.5.2苹果版-西西软件下载
  10. Navicat连接Oracle
  11. php error unexpected,PHP异常Parse error: syntax error, unexpected错误解决方法
  12. 怎么用wps将pdf转换成html,如何将PDF格式的文件转换成HTML格式
  13. 蓝牙耳机厂家新品发布——ANC主动降噪耳机U2065
  14. 验证哥德巴赫猜想:任何一个大于等于6的偶数均可表示为两个素数的和。如6=3+3,8=3+5,,18=5+13。试编写程序,要求将输入的一个偶数表示成两个素数之和。 输入输出样例如下:
  15. DAVIS346动态视觉传感器
  16. 使用密码字典 和 Python自带的pywifi模块穷举WIFI密码
  17. 普林斯顿大学算法公开课笔记
  18. S2B2C 电子商务营销模式
  19. python3爬取知乎某话题下的若干个问题及其回答
  20. .jar文件如何打开_如何干净的清除Windows系统中指定文件的默认打开方式?

热门文章

  1. 能量项链(NOIP-2006 提高组)
  2. 数字反转(信息学奥赛一本通-T1089)
  3. 判断闰年(信息学奥赛一本通-T1055)
  4. python自动化部署工具_Python + Allure(报告)+ Jenkins(持续集成)接口自动化测试环境搭建...
  5. k-means算法的matlab代码实现_MATLAB遗传算法及其实现
  6. new操作符的作用是什么
  7. tf.keras CNN网络搭建笔记
  8. 大白菜安装服务器linux,通过U盘安装Debian
  9. element-UI级联选择器(Cascader)获取label值 - 代码篇
  10. u盘被保护怎样解除?(第十招)