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

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

我们想寻找最好的代码,

我们在想,

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

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

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

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

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

//修改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. 让书写的Matlab代码运行更快 Recipes for Faster Matlab Code

    Matlab 在 Research 中用得非常多,确实也是非常方便实用,只是有一个问题就是写 Matlab 代码的时候经常需要用一些比较奇怪独特的方式来思考和处理问题,否则写出来的代码虽然同样能工作, ...

  2. 直播 NO.5 | Facebook 田渊栋:用深度(强化)学习为组合优化寻找更好的启发式搜索策略...

    ↑↑↑↑↑点击上方蓝色字关注我们! 『运筹OR帷幄』原创 对这次分享主题感兴趣的小伙伴, 欢迎在文末留言提问, 我们会收集有价值的问题, 请嘉宾在直播中亲自解答. 分享提纲 主题:<用深度(强化 ...

  3. 直播 | ACL 2021论文解读:为结构预测问题自动寻找更强的词嵌入拼接方式

    「AI Drive」是由 PaperWeekly 和 biendata 共同发起的学术直播间,旨在帮助更多的青年学者宣传其最新科研成果.我们一直认为,单向地输出知识并不是一个最好的方式,而有效地反馈和 ...

  4. php错误密码也能登陆账号,php用户登陆代码(限制用户错误登录次数)(1/2

    php用户登陆代码(限制用户错误登录次数)(1/2)php用户登陆代码(限制用户错误登录次数) php教程用户登陆代码(限制用户错误登录次数) session_start(); include(&qu ...

  5. VS Code的背景图老是黑白灰等?放一张喜欢的图片当做背景图敲代码不更舒服么

    "废话-哈哈哈" 作为一名程序员,我们要长时间对着电脑敲代码,有时候看代码看的会头晕眼花,但是背景图放一张自己喜欢的图片看代码不更舒服么,但你也不要被背景图吸引,耽误了敲代码,放一 ...

  6. C语言技巧:有if时使用likely和unlikely让代码运行更快

    在单片机/嵌入式编程中,对速度要求比较高,likely和unlikely就是一个比较好的技巧,适用于有if-else分支,且知道哪个发生概率大的情况​. 参考文章:C语言技巧:有if时使用likely ...

  7. 如何优化网站代码SEO更好?

    如何优化网站代码SEO更好? 当前的互联网年代,内容为王,思路为皇,越来越多的站长对自己的网站进行SEO优化,特别是在智能搜索引擎推广方面,内容越来越显得重要,所以越来越多的站长会把更多的心思放在建造 ...

  8. unity 代码热更+资源管理框架总结

    游戏要做热更涉及到什么方面呢 首先就是代码热更,然后就是资源热更 这些热更新都依赖于打AssetBundle 然而打AssetBundle 你还要上传服务器-对比更新-客户端下载-加载-卸载这些流程 ...

  9. MD5校验和资源代码热更(U3D)

    MD5校验和资源代码热更(U3D) 大家都知道,做为游戏开发,现在公司肯定会问你会不会LUA,会不会热更新,等.直入主题哈,今天给大家分享一下游戏中MD5校验和资源代码热更.当然只是从服务器把资源代码 ...

最新文章

  1. java 串口波特率_JAVA串口通信的方法
  2. C++ Primer 5th笔记(6)chapter6 函数: 重载
  3. redis设置为前台运行的方式
  4. oracle11g nid,Oracle工具之nid命令的使用
  5. Zabbix3.2安装
  6. 浅谈SEO翻倍提升网站流量
  7. c# winform gridview 动态按钮_C#窗体Winform,使用实时图表:折线图、柱状图
  8. 泛微OA ecology 您查看的文档过大,请下载文档后查看
  9. 计算机无法删除tf卡的内容,内存卡的文件删不掉怎么办?
  10. c语言中用梯形法求定积分
  11. 这7个关键点,是每个产品用户体验设计的重中之重
  12. 树莓派pico从零开始的入门(一)
  13. composer设置国内源
  14. Sen2Cor-02.05.05处理哨兵数据的坑
  15. 论文写作——如何作图(visio/ppt+Adobe Acrobat Pro)
  16. 油库、加油站、危化企业防雷工程应用方案
  17. python性能分析与优化
  18. pytest入门_测试用例分类_@pytest.mark.smoke
  19. 京东抢购失败?试试用Python准时自动抢购!七夕秒抢种礼物!
  20. 程序员鄙视链最全图解,好有道理

热门文章

  1. 信息学奥赛一本通(1186:出现次数超过一半的数)
  2. 信息学奥赛一本通(1107:校门外的树)
  3. 信息学奥赛一本通(1034:计算三角形面积)
  4. 信息学奥赛一本通(1003:对齐输出)
  5. 奇偶ASCII值判断(信息学奥赛一本通-T1042)
  6. 27 PP配置-生产车间控制-工序-定义确认参数
  7. python用pip安装numpy完整命令_Python使用pip安装Numpy模块
  8. 共享可写节包含重定位_深度探索win32可执行文件格式
  9. Windows10远程桌面连接提示:出现身份验证错误,要求的函数不受支持
  10. linux内核那些事之struct page