<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">博客园_氣如蘭兮長不改，心若蘭兮終不移。</title><subtitle type="text"/><id>http://feed.cnblogs.com/blog/u/14712/rss</id><updated>2012-03-24T02:54:31Z</updated><author><name>君之蘭</name><uri>http://www.cnblogs.com/mad/</uri></author><generator>feed.cnblogs.com</generator><link rel="alternate" type="text/html" href="http://www.cnblogs.com/mad/"/><link rel="self" type="application/atom+xml" href="http://feed.cnblogs.com/blog/u/14712/rss"/><entry><id>http://www.cnblogs.com/mad/archive/2012/03/19/2407107.html</id><title type="text">HTML5 Canvas编写五彩连珠（6）：试玩</title><summary type="text">上节中讲了如何寻路，在和朋友们讨论时都反应有时走的不太对，绕远路了，其实代码主要是大方向的判断 比如目标在右上，那应该是先右还是先上 这个并没有做处理，如果这个做了处理，效果会更好一些，但也难免会走弯路。 贪心就是这样，不是最优，接近最优。也希望其他的同学有意见的可以讨论下。我这也只是个人想法。 既然可以走动了，那就可以判断是否可以消除同样颜色的行、列或斜线了。只要&gt;=5个同样的色球，就清除他们，并且可以继续移动。如果不可以清除，那就再增加3个球。 clearLine: function (x1, y1, color, isClick) { ...</summary><published>2012-03-19T14:56:00Z</published><updated>2012-03-19T14:56:00Z</updated><author><name>君之蘭</name><uri>http://www.cnblogs.com/mad/</uri></author><link rel="alternate" href="http://www.cnblogs.com/mad/archive/2012/03/19/2407107.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/mad/archive/2012/03/19/2407107.html"/><content type="html">&lt;p&gt;上节中讲了如何寻路，在和朋友们讨论时都反应有时走的不太对，绕远路了，其实代码主要是大方向的判断 &amp;nbsp;比如目标在右上，那应该是先右还是先上 这个并没有做处理，如果这个做了处理，效果会更好一些，但也难免会走弯路。 &amp;nbsp;贪心就是这样，不是最优，接近最优。也希望其他的同学有意见的可以讨论下。我这也只是个人想法。&lt;/p&gt;&lt;p&gt;既然可以走动了，那就可以判断是否可以消除同样颜色的行、列或斜线了。只要&amp;gt;=5个同样的色球，就清除他们，并且可以继续移动。如果不可以清除，那就再增加3个球。&amp;nbsp;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;            clearLine: function (x1, y1, color, isClick) {&lt;br/&gt;                if (this.isEmpty(x1, y1)) {&lt;br/&gt;                    if (isClick) game.ready.flyin();&lt;br/&gt;                    return;&lt;br/&gt;                };&lt;br/&gt;                //给定一个坐标，看是否有满足的line可以被消除&lt;br/&gt;                //4根线 一  | / \&lt;br/&gt;                &lt;br/&gt;                var current = this.getBubble(x1, y1);&lt;br/&gt;                if (!current.color) {&lt;br/&gt;                    console.log(current);&lt;br/&gt;                }&lt;br/&gt;                var arr1, arr2, arr3, arr4;&lt;br/&gt;                arr1 = this.bubbles[y1];//横线很简单，就是数组的一项，现成的&lt;br/&gt;&lt;br/&gt;                arr2 = [];&lt;br/&gt;                for (var y = 0; y &amp;lt; game.cellCount; y++)&lt;br/&gt;                    arr2.push(this.getBubble(x1, y));//竖线就是一列。&lt;br/&gt;&lt;br/&gt;                arr3 = [current];&lt;br/&gt;                arr4 = [current];&lt;br/&gt;                for (var i = 1; i &amp;lt; game.cellCount ; i++) {&lt;br/&gt;                    if (x1 - i &amp;gt;= 0 &amp;amp;&amp;amp; y1 - i &amp;gt;= 0)//\斜线的上半部分...&lt;br/&gt;                        arr3.unshift(this.getBubble(x1 - i, y1 - i));&lt;br/&gt;                    if (x1 + i &amp;lt; game.cellCount &amp;amp;&amp;amp; y1 + i &amp;lt; game.cellCount)//\斜线的下半部分&lt;br/&gt;                        arr3.push(this.getBubble(x1 + i, y1 + i));&lt;br/&gt;&lt;br/&gt;                    if (x1 - i &amp;gt;= 0 &amp;amp;&amp;amp; y1 + i &amp;lt; game.cellCount)// /斜线的下半部分&lt;br/&gt;                        arr4.push(this.getBubble(x1 - i, y1 + i));&lt;br/&gt;                    if (x1 + i &amp;lt; game.cellCount &amp;amp;&amp;amp; y1 - i &amp;gt;= 0)// /斜线的上班部分&lt;br/&gt;                        arr4.unshift(this.getBubble(x1 + i, y1 - i));&lt;br/&gt;                }&lt;br/&gt;&lt;br/&gt;                var line1 = getLine(arr1);&lt;br/&gt;                var line2 = getLine(arr2);&lt;br/&gt;                var line3 = getLine(arr3);&lt;br/&gt;                var line4 = getLine(arr4);&lt;br/&gt;&lt;br/&gt;                var line = line1.concat(line2).concat(line3).concat(line4);&lt;br/&gt;                if (line.length &amp;lt; 5) {&lt;br/&gt;                    if (isClick) game.ready.flyin();&lt;br/&gt;                    return;&lt;br/&gt;                }&lt;br/&gt;                else {&lt;br/&gt;                    var me = this;&lt;br/&gt;                    var i = 0;&lt;br/&gt;&lt;br/&gt;                    game.play("clearline", function () {&lt;br/&gt;                        if (i == line.length) {&lt;br/&gt;                            game.score.addScore(line.length);&lt;br/&gt;                            game.stop("clearline");&lt;br/&gt;                            me.isMoving = false;&lt;br/&gt;                            //game.ready.flyin();&lt;br/&gt;                            return;&lt;br/&gt;                        }&lt;br/&gt;                        me.isMoving = true;&lt;br/&gt;                        var p = line[i];&lt;br/&gt;                        me.setBubble(p.x, p.y, null);&lt;br/&gt;                        i++;&lt;br/&gt;                    }, 100);&lt;br/&gt;                }&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;                function getLine(bubbles) {&lt;br/&gt;&lt;br/&gt;                    var line = [];&lt;br/&gt;                    for (var i = 0; i &amp;lt; bubbles.length; i++) {&lt;br/&gt;                        var b = bubbles[i];&lt;br/&gt;                        if (b.color == color) {&lt;br/&gt;                            line.push({ "x": b.x, "y": b.y });&lt;br/&gt;                        }&lt;br/&gt;                        else {&lt;br/&gt;                            if (line.length &amp;lt; 5)&lt;br/&gt;                                line = [];&lt;br/&gt;                            else&lt;br/&gt;                                return line;&lt;br/&gt;                        }&lt;br/&gt;                    }&lt;br/&gt;                    if (line.length &amp;lt; 5)&lt;br/&gt;                        return [];&lt;br/&gt;                    return line;&lt;br/&gt;                }&lt;br/&gt;            },&lt;br/&gt;&lt;/div&gt;&lt;p&gt;大家可以看看代码，主要有两点 1、或许需要处理的 横竖斜线 4个数组，然后看这4个数组是否有连续的。判断是否连续只需要一个状态数组来维持就可以了。&lt;br /&gt;由于考虑到同时消除多行的情况，所以要把连续的line 连接在一起，并在结算得分时考虑奖励。&amp;nbsp;&lt;br /&gt;isClick参数主要用于判断是否是用户点击进行消行的还是新泡泡飞入产生的。 &amp;nbsp;如果达不到消行条件并且是飞入的，那就不能再调用飞入了，否则死循环了。&lt;/p&gt;&lt;p&gt;满足的消行代码也比较简单，就是播放一个消行动画，其实这个动画真心没劲，要想设计好些可以做个渐变偏移消除。 &amp;nbsp;现在我发现起初没有设计一个数组来维护状态有些不太明智，因为还有奖励的星星和炸弹要绘制，挺麻烦的。。 那么，把积分显示出来吧。 这样就可以先试玩起来了：）&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;        game.score = {&lt;br/&gt;            basic: 0,&lt;br/&gt;            operate: 0,&lt;br/&gt;            star1: 0,&lt;br/&gt;            star2: 0,&lt;br/&gt;            boom: 0,&lt;br/&gt;            draw: function () {&lt;br/&gt;                var startX = game.cellWidth * 10 + game.map.startX;&lt;br/&gt;                var startY = game.map.startY;&lt;br/&gt;                var ctx = game.ctx;&lt;br/&gt;                ctx.save();&lt;br/&gt;                ctx.translate(startX, startY);&lt;br/&gt;                ctx.clearRect(0, 0, 150, 400);&lt;br/&gt;                ctx.strokeStyle = "#456";&lt;br/&gt;                //ctx.strokeRect(0, 0, 150, 200);&lt;br/&gt;                ctx.font = "24px 微软雅黑";&lt;br/&gt;                ctx.fillStyle = "#fefefe";&lt;br/&gt;                ctx.fillText("score:" + (this.basic * 5 + this.star1 * 8 + this.star2 * 10 + this.boom * 20), 0, 30);&lt;br/&gt;                ctx.stroke();&lt;br/&gt;                ctx.restore();&lt;br/&gt;            },&lt;br/&gt;            addScore: function (length) {&lt;br/&gt;                switch (length) {&lt;br/&gt;                    case 5:&lt;br/&gt;                        this.basic++;&lt;br/&gt;                        break;&lt;br/&gt;                    case 6:&lt;br/&gt;                        this.star1++;&lt;br/&gt;                        break;&lt;br/&gt;                    case 7:&lt;br/&gt;                        this.star2++;&lt;br/&gt;                        break;&lt;br/&gt;                    default:&lt;br/&gt;                        this.boom++;&lt;br/&gt;                        break;&lt;br/&gt;                }&lt;br/&gt;                this.draw();&lt;br/&gt;                console.log(this.score);&lt;br/&gt;            },&lt;br/&gt;        };&lt;br/&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;第三节在设计里谈到了 百搭星和炸弹，我太懒了，不打算讲了，因为这些处理也没有什么复杂的，HTML5 Canvas的的基础学习也算可以了。后面应该学一些更深入的东西，比如如何处理性能、物体碰撞、运动学神马的。。。&lt;/p&gt;&lt;p&gt;现在游戏应该还有bug，大家可以看看代码发现吧，这个系列就到这了，谢谢大家。&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 16px;"&gt;&lt;strong&gt;&lt;span style="color: #ff0000;"&gt;&lt;a href="http://zhengliangjun.sinaapp.com/colorline.html" target="_blank"&gt;&lt;span style="color: #ff0000;"&gt;试玩地址：http://zhengliangjun.sinaapp.com/colorline.html&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;a href="http://download.csdn.net/download/maddemon/4154990" target="_blank"&gt;&lt;strong&gt;源码下载：http://download.csdn.net/download/maddemon/4154990&amp;nbsp;&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/11157/2012031922560045.jpg" alt="" /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/mad/aggbug/2407107.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/mad/archive/2012/03/19/2407107.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/mad/archive/2012/03/18/2404660.html</id><title type="text">HTML5 Canvas编写五彩连珠（5）：寻路</title><summary type="text">上节主要做了动画的实现，感觉还是比较有意思的。游戏的性能好不好，重绘应该比较重要吧，菜鸟瞎想了下 呵呵。本节就要做对泡泡的操作，上节后面提到了点击泡泡后泡泡要做出闪动响应，那我们我们如何获得被点击了哪个泡泡呢？其实Canvas也是html的一个元素而已，所以我们可以给Canvas加click事件。来查看click时鼠标的坐标，这样就等得出点击了map的哪个位置。我们给game增加一个click方法，当Canvas点击时调用此方法。要实现的效果是： 当Canvas时被点击时有几种可能：1、没点到map 那就不作响应 2、点到了泡泡，那该泡泡要做出响应（闪）3、如果之前有点击过其他的泡泡，则取消之</summary><published>2012-03-18T15:17:00Z</published><updated>2012-03-18T15:17:00Z</updated><author><name>君之蘭</name><uri>http://www.cnblogs.com/mad/</uri></author><link rel="alternate" href="http://www.cnblogs.com/mad/archive/2012/03/18/2404660.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/mad/archive/2012/03/18/2404660.html"/><content type="html">&lt;p&gt;上节主要做了动画的实现，感觉还是比较有意思的。游戏的性能好不好，重绘应该比较重要吧，菜鸟瞎想了下 呵呵。&lt;br /&gt;本节就要做对泡泡的操作，上节后面提到了点击泡泡后泡泡要做出闪动响应，那我们我们如何获得被点击了哪个泡泡呢？&lt;br /&gt;其实Canvas也是html的一个元素而已，所以我们可以给Canvas加click事件。来查看click时鼠标的坐标，这样就等得出点击了map的哪个位置。&lt;br /&gt;我们给game增加一个click方法，当Canvas点击时调用此方法。&lt;br /&gt;要实现的效果是： 当Canvas时被点击时有几种可能：&lt;br /&gt;1、没点到map &amp;nbsp;那就不作响应 &lt;br /&gt;2、点到了泡泡，那该泡泡要做出响应（闪）&lt;br /&gt;3、如果之前有点击过其他的泡泡，则取消之前的泡泡的响应（clicked.stop)，如果之前的泡泡是自己，则不作响应。并把clicked作为自己，以体后面的操作。&lt;br /&gt;4、如果点击到的是空格，如果之前点击了泡泡，那就尝试移动这个泡泡，如果clicked为null（之前没泡泡）那就不作任何响应。如果可以移动，则取消闪动，并清除clicked，开始移动。&amp;nbsp;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;            onclick: function (e) {&lt;br/&gt;                var px = e.offsetX - game.map.startX;&lt;br/&gt;                var py = e.offsetY - game.map.startY;&lt;br/&gt;                if (px &amp;lt; 0 || py &amp;lt; 0 || px &amp;gt; game.map.width || py &amp;gt; game.map.height) {&lt;br/&gt;                    return;&lt;br/&gt;                }&lt;br/&gt;                var x = parseInt(px / game.cellWidth);&lt;br/&gt;                var y = parseInt(py / game.cellWidth);&lt;br/&gt;&lt;br/&gt;                var bubble = game.map.getBubble(x, y);&lt;br/&gt;                if (bubble.color) {&lt;br/&gt;                    if (this.clicked) {&lt;br/&gt;                        //同一个泡不做反映&lt;br/&gt;                        if (this.clicked.x == x &amp;amp;&amp;amp; this.clicked.y == y) {&lt;br/&gt;                            return;&lt;br/&gt;                        }&lt;br/&gt;                        this.clicked.stop();&lt;br/&gt;                    }&lt;br/&gt;                    this.clicked = bubble;&lt;br/&gt;                    bubble.play();&lt;br/&gt;                }&lt;br/&gt;                else {&lt;br/&gt;                    if (this.clicked) {&lt;br/&gt;                        this.clicked.stop();&lt;br/&gt;                        //移动clicked&lt;br/&gt;                        game.map.move(this.clicked, bubble);&lt;br/&gt;                    }&lt;br/&gt;                }&lt;br/&gt;                //console.log("x:" + x + " y:" + y);&lt;br/&gt;            },&lt;/div&gt;&lt;p&gt;寻路的代码还没写，因为这个需要考虑怎么实现。 我绞尽脑汁终于想到了一个办法。暂且撇开游戏的代码，单独实现下两点的寻路代码。&lt;br /&gt;先给定一个棋盘，假如如下：&lt;br /&gt;1 1 1 1 1&lt;br /&gt;0 0 1 0 1&lt;br /&gt;0 0 1 0 1&lt;br /&gt;1 0 0 1 1&lt;br /&gt;要想从 最下面一行中间的点（2，3）移动到左上角的（0，1），该如何设计呢？&lt;br /&gt;一个棋子能否移动，要看他相邻的4个子是否为0，如果是0则可以移动。 所以我们可以通过递归来获得所有相连的0的记录。 这个记录用树结构来存储，直到我们无法继续探测为0的格子或到达目的地。 我们把当前的棋子的格子设为 root，他相邻的棋子是他的孩子。这样的话，我们会得到一棵树的结果如下：&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/11157/2012031815010369.jpg" alt="" /&gt;&lt;/p&gt;&lt;p&gt;是不是？这样的画我们就可以直接看到了整个路径(2,3 -&amp;gt; 1,3 -&amp;gt; 1,2 -&amp;gt; 0,2 -&amp;gt; 0,1)。思路很清晰，只要递归构建子节点就ok了。代码如下：&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;    var map = [&lt;br/&gt;[1, 1, 1, 1, 1],&lt;br/&gt;[0, 0, 1, 0, 1],&lt;br/&gt;[0, 0, 1, 0, 1],&lt;br/&gt;[1, 0, 0, 1, 1]&lt;br/&gt;    ];&lt;br/&gt;&lt;br/&gt;    var history = [];&lt;br/&gt;    var goal = { "x": 0, "y": 1 }&lt;br/&gt;    var goalNode = null;&lt;br/&gt;    var getNode = function (x, y, parent) {&lt;br/&gt;        if (x &amp;gt;= map.length || y &amp;gt;= map.length) {&lt;br/&gt;            return;&lt;br/&gt;        }&lt;br/&gt;&lt;br/&gt;        if (map[y][x] == 1) {&lt;br/&gt;            return;&lt;br/&gt;        }&lt;br/&gt;&lt;br/&gt;        var hasNode = false;&lt;br/&gt;        history.forEach(function (n) {&lt;br/&gt;            if (n.x == x &amp;amp;&amp;amp; n.y == y) {&lt;br/&gt;                hasNode = true;&lt;br/&gt;                return;&lt;br/&gt;            }&lt;br/&gt;        });&lt;br/&gt;&lt;br/&gt;        if (hasNode) {&lt;br/&gt;            return;&lt;br/&gt;        }&lt;br/&gt;        var node = { "x": x, "y": y, "parent": parent, child: [] };&lt;br/&gt;        history.push(node);&lt;br/&gt;&lt;br/&gt;        if (node.x == goal.x &amp;amp;&amp;amp; node.y == goal.y) {&lt;br/&gt;            goalNode = node;&lt;br/&gt;            return node;&lt;br/&gt;        }&lt;br/&gt;        if (x - 1 &amp;gt;= 0 &amp;amp;&amp;amp; !map[y][x - 1]) {&lt;br/&gt;            node.child.push(getNode(x - 1, y, node));&lt;br/&gt;        }&lt;br/&gt;        if (y - 1 &amp;gt;= 0 &amp;amp;&amp;amp; !map[y - 1][x]) {&lt;br/&gt;            node.child.push(getNode(x, y - 1, node));&lt;br/&gt;        }&lt;br/&gt;        if (x + 1 &amp;lt; map.length &amp;amp;&amp;amp; !map[y][x + 1]) {&lt;br/&gt;            node.child.push(getNode(x + 1, y), node);&lt;br/&gt;        }&lt;br/&gt;        if (y + 1 &amp;lt; map.length &amp;amp;&amp;amp; !map[y + 1][x]) {&lt;br/&gt;            node.child.push(getNode(x, y + 1, node));&lt;br/&gt;        }&lt;br/&gt;        return node;&lt;br/&gt;    }&lt;br/&gt;    console.log(getNode(2, 3));&lt;br/&gt;    console.log(goalNode);&lt;/div&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;我加了一个parent，就是指向父亲的指针，那样就不用再去遍历这棵树了。可以直接从goalNode的结果得到整个路径：） 虽然偷懒，但也是要复出代价的，因为这样走的路径不是最短路线，比较傻，怎么选择最优路线呢？ 最笨的方法就是 把所有的路径都得到（深度优先遍历树N遍- -）然后比较。这个显然效率不高。开始我也不知道效果会这么差，等一运行（你运行下就知道了），我发现，是代码写的不好（废话）。因为我们每次的判断顺寻都是 左上右下，这样路径总是这个方向探索，而最优的路径应该是朝目标点的方向探索。 由于是递归查找，所以，对当前的node和目标node进行坐标的方向判断，然后调整判断顺序，这样是得到的才是比较短的路径。&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;var child = [];&lt;br/&gt;var left, top, right, buttom;&lt;br/&gt;//最短路径的粗略判断就是首选目标位置的大致方向&lt;br/&gt;if (x - 1 &amp;gt;= 0 &amp;amp;&amp;amp; map.isEmpty(x - 1, y))&lt;br/&gt;left = { "x": x - 1, "y": y };&lt;br/&gt;if (x + 1 &amp;lt; map.length &amp;amp;&amp;amp; map.isEmpty(x + 1, y))&lt;br/&gt;right = { "x": x + 1, "y": y };&lt;br/&gt;if (y + 1 &amp;lt; map.length &amp;amp;&amp;amp; map.isEmpty(x, y + 1))&lt;br/&gt;buttom = { "x": x, "y": y + 1 };&lt;br/&gt;if (y - 1 &amp;gt;= 0 &amp;amp;&amp;amp; map.isEmpty(x, y - 1))&lt;br/&gt;top = { "x": x, "y": y - 1 };&lt;br/&gt;&lt;br/&gt;if (x &amp;gt; x2) {&lt;br/&gt;if (y &amp;gt; y2)&lt;br/&gt;child = [left, top, right, buttom];&lt;br/&gt;else if (y &amp;lt; y2)&lt;br/&gt;child = [left, buttom, right, top];&lt;br/&gt;else&lt;br/&gt;child = [left, top, right, buttom];&lt;br/&gt;}&lt;br/&gt;else if (x &amp;lt; x2) {&lt;br/&gt;if (y &amp;gt; y2)&lt;br/&gt;child = [right, top, left, buttom];&lt;br/&gt;else if (y &amp;lt; y2)&lt;br/&gt;child = [right, buttom, left, top];&lt;br/&gt;else&lt;br/&gt;child = [right, top, left, buttom];&lt;br/&gt;}&lt;br/&gt;else if (x == x2) {&lt;br/&gt;if (y &amp;gt; y2)&lt;br/&gt;child = [top, left, right, buttom];&lt;br/&gt;else if (y &amp;lt; y2)&lt;br/&gt;child = [buttom, left, right, top];&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;for (var i = 0; i &amp;lt; child.length; i++) {&lt;br/&gt;var c = child[i];&lt;br/&gt;if (c) node.child.push(getnode(c.x, c.y, node));&lt;br/&gt;}&lt;br/&gt;&lt;/div&gt;&lt;p&gt;代码虽然写的比较傻，但这种方式不得不说好就一个字：）&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&amp;nbsp;既然寻路已经实现了，那么下面就交给map了，map来负责让泡泡走起来。其实就是根据路径给泡泡着色- - ，代码也不复杂。&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;            move: function (bubble, target) {&lt;br/&gt;                var path = this.search(bubble.x, bubble.y, target.x, target.y);&lt;br/&gt;                if (!path) {&lt;br/&gt;                    //显示不能移动s&lt;br/&gt;                    alert("过不去");&lt;br/&gt;                    return;&lt;br/&gt;                }&lt;br/&gt;                //map开始播放当前泡的移动效果&lt;br/&gt;                //两种实现方式，1、map按路径染色，最后达到目的地 2、map生成一个临时的bubble负责展示，到目的地后移除&lt;br/&gt;                //console.log(path);&lt;br/&gt;                var me = this;&lt;br/&gt;                var name = "move_" + bubble.x + "_" + bubble.y;&lt;br/&gt;                var i = path.length - 1;&lt;br/&gt;                var color = bubble.color;&lt;br/&gt;                game.play(name, function () {&lt;br/&gt;                    if (i &amp;lt; 0) {&lt;br/&gt;                        game.stop(name);&lt;br/&gt;                        return;&lt;br/&gt;                    }&lt;br/&gt;                    path.forEach(function (cell) {&lt;br/&gt;                        me.setBubble(cell.x, cell.y, null);&lt;br/&gt;                    });&lt;br/&gt;                    var currentCell = path[i];&lt;br/&gt;                    me.setBubble(currentCell.x, currentCell.y, color);&lt;br/&gt;                    i--;&lt;br/&gt;&lt;br/&gt;                }, 50);&lt;br/&gt;            },&lt;br/&gt;            search: function (x1, y1, x2, y2) {&lt;br/&gt;&lt;br/&gt;                var history = [];&lt;br/&gt;                var goalCell = null;&lt;br/&gt;                var me = this;&lt;br/&gt;&lt;br/&gt;                getCell(x1, y1, null);&lt;br/&gt;                if (goalCell) {&lt;br/&gt;                    var path = [];&lt;br/&gt;&lt;br/&gt;                    var cell = goalCell;&lt;br/&gt;                    while (cell) {&lt;br/&gt;                        path.push({ "x": cell.x, "y": cell.y });&lt;br/&gt;                        cell = cell.parent;&lt;br/&gt;                    }&lt;br/&gt;&lt;br/&gt;                    return path;&lt;br/&gt;                }&lt;br/&gt;                return null;&lt;br/&gt;&lt;br/&gt;                function getCell(x, y, parent) {&lt;br/&gt;                    if (x &amp;gt;= me.bubbles.length || y &amp;gt;= me.bubbles.length)&lt;br/&gt;                        return;&lt;br/&gt;                    if (x != x1 &amp;amp;&amp;amp; y != y2 &amp;amp;&amp;amp; !me.isEmpty(x, y))&lt;br/&gt;                        return;&lt;br/&gt;&lt;br/&gt;                    for (var i = 0; i &amp;lt; history.length; i++) {&lt;br/&gt;                        if (history[i].x == x &amp;amp;&amp;amp; history[i].y == y)&lt;br/&gt;                            return;&lt;br/&gt;                    }&lt;br/&gt;&lt;br/&gt;                    var cell = { "x": x, "y": y, child: [], "parent": parent };&lt;br/&gt;                    history.push(cell);&lt;br/&gt;&lt;br/&gt;                    if (cell.x == x2 &amp;amp;&amp;amp; cell.y == y2) {&lt;br/&gt;                        goalCell = cell;&lt;br/&gt;                        return cell;&lt;br/&gt;                    }&lt;br/&gt;&lt;br/&gt;                    var child = [];&lt;br/&gt;                    var left, top, right, buttom;&lt;br/&gt;                    //最短路径的粗略判断就是首选目标位置的大致方向&lt;br/&gt;                    if (x - 1 &amp;gt;= 0 &amp;amp;&amp;amp; me.isEmpty(x - 1, y))&lt;br/&gt;                        left = { "x": x - 1, "y": y };&lt;br/&gt;                    if (x + 1 &amp;lt; me.bubbles.length &amp;amp;&amp;amp; me.isEmpty(x + 1, y))&lt;br/&gt;                        right = { "x": x + 1, "y": y };&lt;br/&gt;                    if (y + 1 &amp;lt; me.bubbles.length &amp;amp;&amp;amp; me.isEmpty(x, y + 1))&lt;br/&gt;                        buttom = { "x": x, "y": y + 1 };&lt;br/&gt;                    if (y - 1 &amp;gt;= 0 &amp;amp;&amp;amp; me.isEmpty(x, y - 1))&lt;br/&gt;                        top = { "x": x, "y": y - 1 };&lt;br/&gt;&lt;br/&gt;                    if (x &amp;gt; x2) {&lt;br/&gt;                        if (y &amp;gt; y2)&lt;br/&gt;                            child = [left, top, right, buttom];&lt;br/&gt;                        else if (y &amp;lt; y2)&lt;br/&gt;                            child = [left, buttom, right, top];&lt;br/&gt;                        else&lt;br/&gt;                            child = [left, top, right, buttom];&lt;br/&gt;                    }&lt;br/&gt;                    else if (x &amp;lt; x2) {&lt;br/&gt;                        if (y &amp;gt; y2)&lt;br/&gt;                            child = [right, top, left, buttom];&lt;br/&gt;                        else if (y &amp;lt; y2)&lt;br/&gt;                            child = [right, buttom, left, top];&lt;br/&gt;                        else&lt;br/&gt;                            child = [right, top, left, buttom];&lt;br/&gt;                    }&lt;br/&gt;                    else if (x == x2) {&lt;br/&gt;                        if (y &amp;gt; y2)&lt;br/&gt;                            child = [top, left, right, buttom];&lt;br/&gt;                        else if (y &amp;lt; y2)&lt;br/&gt;                            child = [buttom, left, right, top];&lt;br/&gt;                    }&lt;br/&gt;&lt;br/&gt;                    for (var i = 0; i &amp;lt; child.length; i++) {&lt;br/&gt;                        var c = child[i];&lt;br/&gt;                        if (c) cell.child.push(getCell(c.x, c.y, cell));&lt;br/&gt;                    }&lt;br/&gt;                    return cell;&lt;br/&gt;                }&lt;br/&gt;            },&lt;br/&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;&lt;span&gt;&lt;a href="http://zhengliangjun.sinaapp.com/colorline.html" target="_blank"&gt;&lt;span&gt;试玩地址：http://zhengliangjun.sinaapp.com/colorline.html&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;后面剩下的就是判断如何消除、加分、防止误操作之类的内容了。&lt;/p&gt;&lt;img src="http://www.cnblogs.com/mad/aggbug/2404660.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/mad/archive/2012/03/18/2404660.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/mad/archive/2012/03/18/2403404.html</id><title type="text">HTML5 Canvas编写五彩连珠（4）：动画</title><summary type="text">上一节中，我们留下了一个flyin的方法没有介绍，这里想单独写一篇html5的动画实现。在第二节中我们实现了画一个泡泡，并且成功的擦除了泡泡，但当时也说了别把棋盘的线给擦掉了，所以做了偏移量。所以说html5 Canvas还是低级， 没有图层的概念，擦掉再想补回来，怎么补？ 答案就是重绘。 没错，整个Canvas重绘，这样就能不用担心补哪里了。虽然带来了性能的损失，但绝对减少的编码难度。而且计算机的能力也不差这点损失。那么重绘的话，我们在Canvas是上所有的需要绘制的对象都应该有draw方法。这是必须的。另外，所有的元素都有个上下的概念，所以要先绘制下面的，再绘制上面的。 而这个上下就得靠 </summary><published>2012-03-18T06:30:00Z</published><updated>2012-03-18T06:30:00Z</updated><author><name>君之蘭</name><uri>http://www.cnblogs.com/mad/</uri></author><link rel="alternate" href="http://www.cnblogs.com/mad/archive/2012/03/18/2403404.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/mad/archive/2012/03/18/2403404.html"/><content type="html">&lt;p&gt;上一节中，我们留下了一个flyin的方法没有介绍，这里想单独写一篇html5的动画实现。&lt;br /&gt;在第二节中我们实现了画一个泡泡，并且成功的擦除了泡泡，但当时也说了别把棋盘的线给擦掉了，所以做了偏移量。所以说html5 Canvas还是低级， 没有图层的概念，擦掉再想补回来，怎么补？ &amp;nbsp;答案就是重绘。 &amp;nbsp;没错，整个Canvas重绘，这样就能不用担心补哪里了。虽然带来了性能的损失，但绝对减少的编码难度。而且计算机的能力也不差这点损失。那么重绘的话，我们在Canvas是上所有的需要绘制的对象都应该有draw方法。这是必须的。另外，所有的元素都有个上下的概念，所以要先绘制下面的，再绘制上面的。 而这个上下就得靠 子元素的概念，这样在父元素绘制完毕后遍历其子元素绘制，就不用担心掩盖的问题。&lt;br /&gt;&lt;br /&gt;如果想把ready区的3个泡&amp;ldquo;飞入&amp;rdquo;棋盘，就需要让Canvas在泡移动的时候进行重绘，泡泡不动时不需要重绘。泡泡的移动很容易实现，只要改变他的x,y坐标即可。如果想达到动画的效果，就得在改变坐标期间，定时重绘，可以使用setInterval来实现。&lt;br /&gt;另外，我们不光飞入的动作需要重绘，游戏开始后玩家还要点击移动一个泡泡到另外一个格子，所以这里也要重绘。那绘制的信息这么多，整个重绘工作都要交给game来进行，game控制所有的父元素。&amp;nbsp;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;start: function () {&lt;br/&gt;this.map.init();&lt;br/&gt;this.ready.init();&lt;br/&gt;this.draw();&lt;br/&gt;this.canvas.onclick = this.onclick;&lt;br/&gt;},&lt;br/&gt;over: function () {&lt;br/&gt;alert("GAME OVER");&lt;br/&gt;},&lt;br/&gt;draw: function () {&lt;br/&gt;this.ctx.clearRect(0, 0, 400, 600);&lt;br/&gt;this.ctx.save();&lt;br/&gt;this.map.draw();&lt;br/&gt;this.ready.draw();&lt;br/&gt;this.ctx.restore();&lt;br/&gt;},&lt;br/&gt;play: function (action, interval) {&lt;br/&gt;var me = this;&lt;br/&gt;play = setInterval(function () {&lt;br/&gt;action();&lt;br/&gt;me.draw();&lt;br/&gt;}, interval || 1);&lt;br/&gt;},&lt;br/&gt;stop: function () {&lt;br/&gt;clearInterval(play);&lt;br/&gt;this.draw();&lt;br/&gt;//console.log(this.ready.bubbles.length);&lt;br/&gt;},&lt;br/&gt;&lt;/div&gt;&lt;p&gt;game.start就是初始化所有的父元素，&lt;br /&gt;game.over自然不必说，只是这里没有写具体代码，结束时应该无法继续操作泡泡。&lt;br /&gt;game.draw 绘制所有的父元素&lt;br /&gt;game.play 就是重绘方法，需要重绘时掉用此方法。接收2个参数，第一个是重绘时需要做的动作，interval是绘制的间隔时间。不同的动作可能间隔不一样。&lt;br /&gt;可能这种实现是野路子，真正的重绘应该是游戏开始后就不听的调用重绘方法，而不是具体哪里调用，只是在具体的精灵（每个元素）Update自己状态就像我这里的action。 &amp;nbsp;这里我们暂且这样实现，后面如果达不到需求再重构这个重绘的代码，毕竟核心的代码不变，只是改改机制 不是大问题。&lt;br /&gt;game.stop 停止重绘，又调用了一次draw，是为了保证最后的绘制没问题。&lt;br /&gt;&lt;br /&gt;接下来考虑下flyin飞入的实现。知道起始和结束的xy坐标，飞入的路径不是问题，无非是x y的加加减减，那么动画方面，我们的game.play的action就是来加减ready.bubbles的xy坐标。飞入的逻辑并非这么简单，首先需要3个没染色空格，如果不足3个，那就GameOver了，所以map需要提供一个返回3个空格子的方法，另外，飞入之后ready区要重新生成新的泡泡，而这几个被飞的泡泡需要删除，并且map要对3个空格子进行染色。 这就完成了整个飞入效果。&lt;br /&gt;其实还有一个逻辑就是 飞入完毕后检查是否有哪些泡泡可以被消除，这个我们后面再讲。&lt;br /&gt;先看获取3个空格的方法：&amp;nbsp;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;            getEmptyBubbles: function () {&lt;br/&gt;                var empties = [];&lt;br/&gt;                this.bubbles.forEach(function (row) {&lt;br/&gt;                    row.forEach(function (bubble) {&lt;br/&gt;                        if (!bubble.color) {&lt;br/&gt;                            empties.push(new Bubble(bubble.x, bubble.y));&lt;br/&gt;                        }&lt;br/&gt;                    });&lt;br/&gt;                });&lt;br/&gt;                if (empties.length &amp;lt;= 3) {&lt;br/&gt;                    game.over();&lt;br/&gt;                    return [];&lt;br/&gt;                }&lt;br/&gt;&lt;br/&gt;                var result = [];&lt;br/&gt;                var useds = [];&lt;br/&gt;                for (var i = 0; i &amp;lt; empties.length; i++) {&lt;br/&gt;                    if (result.length == 3) {&lt;br/&gt;                        break;&lt;br/&gt;                    }&lt;br/&gt;                    var isUsed = false;&lt;br/&gt;                    var ra = game.getRandom(empties.length);&lt;br/&gt;                    for (var m = 0; m &amp;lt; useds.length; m++) {&lt;br/&gt;                        isUsed = ra === useds[m];&lt;br/&gt;                        if (isUsed) break;&lt;br/&gt;                    }&lt;br/&gt;                    if (!isUsed) {&lt;br/&gt;                        result.push(empties[ra]);&lt;br/&gt;                        useds.push(ra);&lt;br/&gt;                    }&lt;br/&gt;                }&lt;br/&gt;                console.log(useds);&lt;br/&gt;                return result;&lt;br/&gt;            },&lt;/div&gt;&lt;p&gt;获取3个随机的空格还是挺多代码的。。。然后就是flyin的实现了。&lt;/p&gt;&lt;p&gt;首先定一个一个status，来存飞入的状态。3个都飞完毕才能做后面的逻辑。Bubble对象也为此增加了px和py俩个成员（即Bubble的实际坐标），这样才能控制每个像素的移动。 其实在计算飞入路径时我写了很久的代码，别看现在就这么几行，开发过程中还是颇费力。各种诡异的飞行。。。开始是按x++ y++递增飞行的，这样就是45&amp;deg;角飞行，但显然飞行线路（起始到结束的线）的倾斜度不是45&amp;deg;，那就会出现先飞完x或y，再走直线，很傻的。所以要用斜率来计算当前的y坐标。而x的坐标可以固定常熟移动。我画了一个斜率的公式，忘记的同学可以看看下。根据长宽的比率，就能计算当前的y值。&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/11157/2012031813032840.jpg" alt="" /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;flyin: function () {&lt;br/&gt;var emptys = game.map.getEmptyBubbles();&lt;br/&gt;if (emptys.length &amp;lt; 3) {&lt;br/&gt;//GAME OVER&lt;br/&gt;game.over();&lt;br/&gt;return;&lt;br/&gt;}&lt;br/&gt;var me = this;&lt;br/&gt;var status = [0, 0, 0];&lt;br/&gt;game.play(function () {&lt;br/&gt;if (status[0] &amp;amp;&amp;amp; status[1] &amp;amp;&amp;amp; status[2]) {&lt;br/&gt;game.stop();&lt;br/&gt;status = [0, 0, 0];&lt;br/&gt;me.bubbles = [];&lt;br/&gt;me.genrate();&lt;br/&gt;return;&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;for (var i = 0; i &amp;lt; me.bubbles.length; i++) {&lt;br/&gt;if (status[i]) {&lt;br/&gt;continue;&lt;br/&gt;}&lt;br/&gt;var target = emptys[i];&lt;br/&gt;var x2 = target.px + game.map.startX - me.startX;&lt;br/&gt;var y2 = target.py + game.map.startY - me.startY;&lt;br/&gt;&lt;br/&gt;var current = me.bubbles[i];&lt;br/&gt;&lt;br/&gt;var tmpWidth = 2;&lt;br/&gt;if (current.px &amp;lt; x2) {&lt;br/&gt;current.py = ((y2 - current.py) / (x2 - current.px)) * tmpWidth + current.py;&lt;br/&gt;current.px += tmpWidth;&lt;br/&gt;}&lt;br/&gt;else if (current.px &amp;gt; x2) {&lt;br/&gt;current.py = ((y2 - current.py) / (current.px - x2)) * tmpWidth + current.py;&lt;br/&gt;current.px -= tmpWidth;&lt;br/&gt;}&lt;br/&gt;else {&lt;br/&gt;current.py += tmpWidth;&lt;br/&gt;}&lt;br/&gt;if (current.py &amp;gt; y2) {&lt;br/&gt;current.py = y2;&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;if (current.px &amp;gt; x2) {&lt;br/&gt;current.px = x2;&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;if (current.px == x2 &amp;amp;&amp;amp; current.py == y2) {&lt;br/&gt;status[i] = 1;&lt;br/&gt;current.x = target.x;&lt;br/&gt;current.y = target.y;&lt;br/&gt;game.map.addBubble(current);&lt;br/&gt;console.log(1);&lt;br/&gt;}&lt;br/&gt;}&lt;br/&gt;});&lt;br/&gt;&lt;br/&gt;}&lt;/div&gt;&lt;p&gt;既然我们已经实现了动画效果，那么接下来顺便再实现一个动画效果，就是点击泡泡时，泡泡要做出响应（就是忽闪忽闪的），要不然用户都不知道点了没有。所以Bubble也要增加一个闪动的action。&lt;/p&gt;&lt;p&gt;代码的意思是让间隔50毫秒，重绘一次光照的亮度，亮度值（外圆的半径）10和30之间来回荡。这样就亮了暗，暗了再亮。很有意思：）&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;Bubble.prototype.play = function () {&lt;br/&gt;var me = this;&lt;br/&gt;var isUp = true;&lt;br/&gt;game.play("light_" + this.x + "_" + this.y, function () {&lt;br/&gt;if (isUp) {&lt;br/&gt;me.light++;&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;if (!isUp) {&lt;br/&gt;me.light--;&lt;br/&gt;}&lt;br/&gt;if (me.light == 30) {&lt;br/&gt;isUp = false;&lt;br/&gt;}&lt;br/&gt;if (me.light == 10) {&lt;br/&gt;isUp = true;&lt;br/&gt;}&lt;br/&gt;}, 50);&lt;br/&gt;};&lt;br/&gt;&lt;br/&gt;Bubble.prototype.stop = function () {&lt;br/&gt;//this.light = 10;&lt;br/&gt;var me = this;&lt;br/&gt;game.stop("light_" + this.x + "_" + this.y);&lt;br/&gt;game.play("restore_" + this.x + "_" + this.y, function () {&lt;br/&gt;if (me.light &amp;gt; 10) {&lt;br/&gt;me.light--;&lt;br/&gt;}&lt;br/&gt;else {&lt;br/&gt;me.light = 10;&lt;br/&gt;game.stop("restore_" + me.x + "_" + me.y);&lt;br/&gt;}&lt;br/&gt;}, 50);&lt;br/&gt;};&lt;br/&gt;&lt;/div&gt;&lt;p&gt;细心的朋友可能会发现，在调用Game.stop的方法的参数上增加了一个参数。 这里我要说明下。如果没有参数的情况，game.play和stop会造成问题，因为用的都是一个interval，clear的话会打断其他的动画，所以我们把每个action都要传递一个name，这样就能让game粒度更细的控制每个action了。game的播放代码也做了响应的调整，如下：&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;            play: function (name, action, interval) {&lt;br/&gt;                var me = this;&lt;br/&gt;                this.actions[name] = setInterval(function () {&lt;br/&gt;                    action();&lt;br/&gt;                    me.draw();&lt;br/&gt;                }, interval || 1);&lt;br/&gt;            },&lt;br/&gt;            stop: function (name) {&lt;br/&gt;                clearInterval(this.actions[name]);&lt;br/&gt;                this.draw();&lt;br/&gt;            },&lt;br/&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;效果演示地址：&lt;a href="http://jsfiddle.net/maddemon/VtMSU/embedded/result/" target="_blank"&gt;http://jsfiddle.net/maddemon/VtMSU/embedded/result/&lt;/a&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/mad/aggbug/2403404.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/mad/archive/2012/03/18/2403404.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/mad/archive/2012/03/17/2392632.html</id><title type="text">HTML5 Canvas编写五彩连珠（3）：设计</title><summary type="text">在看了几篇Canvas相关的文章后，发现前两节的代码实现还是有问题，因为知道的少，所以只能在自己已知的知识上做实现。不过还好，这是一个发现的过程，也是一个纠错和完善的过程。我第一次尝试一边学习一遍写博客，我想这也有助我的学习，可以把知识掌握的牢固些，起码忘的慢一些吧：）。 前两节学习了几个基本绘制的方法，lineTo moveTo和arc，也了解坐标的情况，但写的比较傻，只是单纯的实现。 比如棋盘的起始坐标如果有偏移量，我们还要计算他的具体开始坐标和结束坐标，实际上Canvas有现有的方法提供偏移的功能。 他叫 translate，另外还有缩放scale、旋转rotate，他们都可以用t...</summary><published>2012-03-17T09:24:00Z</published><updated>2012-03-17T09:24:00Z</updated><author><name>君之蘭</name><uri>http://www.cnblogs.com/mad/</uri></author><link rel="alternate" href="http://www.cnblogs.com/mad/archive/2012/03/17/2392632.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/mad/archive/2012/03/17/2392632.html"/><content type="html">&lt;p&gt;在看了几篇Canvas相关的文章后，发现前两节的代码实现还是有问题，因为知道的少，所以只能在自己已知的知识上做实现。不过还好，这是一个发现的过程，也是一个纠错和完善的过程。我第一次尝试一边学习一遍写博客，我想这也有助我的学习，可以把知识掌握的牢固些，起码忘的慢一些吧：）。&lt;/p&gt;&lt;p&gt;前两节学习了几个基本绘制的方法，lineTo moveTo和arc，也了解坐标的情况，但写的比较傻，只是单纯的实现。 比如棋盘的起始坐标如果有偏移量，我们还要计算他的具体开始坐标和结束坐标，实际上Canvas有现有的方法提供偏移的功能。 他叫 translate，另外还有缩放scale、旋转rotate，他们都可以用transform代替。所以，在代码方面还会有些调整。不过这个的学习恰巧也让我知道如何实现动画效果。如果多个元素在一个Canvas上，实现动画，必然会需要擦除重绘的情况，如果元素之间有覆盖的情况，擦除就需要多考虑了。当然，简单的办法就是把整个画布根据当然所有元素的位置重新绘制一遍。所以在代码设计方面，需要把不同的元素独立出来，每个元素都有自己的draw方法，并且要依照次序绘制Canvas。&lt;/p&gt;&lt;p&gt;分析一下游戏所需的元素：1、棋盘（地图）2、泡泡 3、等待区域（新的3个即将进入棋盘的泡泡）4、奖励区域（白搭星、超级百搭星、炸弹）5、统计信息 6、按钮&lt;br /&gt;所以在对象的设计方面起码要有几类 棋盘(map)、新泡泡区（ready）、奖励区（awards）、泡泡（bubble）、星星1(star1) 、星星2(star2) 、炸弹(boom)、统计积分（score），还要包括游戏背后的数据（data）。 OK，先这么规划，挨个的去实现。先把map和bubble重写了。&amp;nbsp;&lt;br /&gt;之前把map写成了类，显然是不合适的，因为这个游戏不可能会有多个map，所以直接定义为对象更方便。而泡泡显然需要很多，所以需要写成类比较方便。游戏里面所有对象需要访问的全局变量和常量需要定义在一个game对象里，游戏开始则是调用game.start()的方法。所以先看下game的定义：&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;var game = {&lt;br/&gt;canvas: document.getElementById("canvas"),&lt;br/&gt;ctx: this.canvas.getContext("2d"),&lt;br/&gt;cellCount: 9,&lt;br/&gt;cellWidth: 30,&lt;br/&gt;lineCount: 5,&lt;br/&gt;mode: 7,&lt;br/&gt;colors: ["red", "#039518", "#ff00dc", "#ff6a00", "gray", "#0094ff", "#d2ce00"],&lt;br/&gt;over: function () {&lt;br/&gt;alert("GAME OVER");&lt;br/&gt;},&lt;br/&gt;getRandom: function (max) {&lt;br/&gt;return parseInt(Math.random() * 1000000 % (max));&lt;br/&gt;},&lt;br/&gt;};&lt;br/&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;cellCount就是格子的总数，cellwidth是每个格子的宽度，因为不光map里需要这个，所以就定义在了这里，mode 是游戏模式 5是简单 7是困难。&lt;br /&gt;&amp;nbsp;再看下map的代码：&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;game.map = {&lt;br/&gt;startX: 40.5,&lt;br/&gt;startY: 60.5,&lt;br/&gt;width: game.cellCount * game.cellWidth,&lt;br/&gt;height: game.cellCount * game.cellWidth,&lt;br/&gt;bubbles: [],&lt;br/&gt;init: function () {&lt;br/&gt;for (var i = 0; i &amp;lt; game.cellCount; i++) {&lt;br/&gt;var row = [];&lt;br/&gt;for (var j = 0; j &amp;lt; game.cellCount; j++) {&lt;br/&gt;row.push(new Bubble(i, j, null));&lt;br/&gt;}&lt;br/&gt;this.bubbles.push(row);&lt;br/&gt;}&lt;br/&gt;},&lt;br/&gt;draw: function () {&lt;br/&gt;var ctx = game.ctx;&lt;br/&gt;ctx.save();&lt;br/&gt;ctx.translate(this.startX, this.startY);&lt;br/&gt;ctx.beginPath();&lt;br/&gt;for (var i = 0; i &amp;lt;= 9; i++) {&lt;br/&gt;&lt;br/&gt;var p1 = i * game.cellWidth;;&lt;br/&gt;ctx.moveTo(p1, 0);&lt;br/&gt;ctx.lineTo(p1, this.height);&lt;br/&gt;&lt;br/&gt;var p2 = i * game.cellWidth;&lt;br/&gt;ctx.moveTo(0, p2);&lt;br/&gt;ctx.lineTo(this.width, p2);&lt;br/&gt;}&lt;br/&gt;ctx.strokeStyle = "#555";&lt;br/&gt;ctx.stroke();&lt;br/&gt;&lt;br/&gt;//绘制子元素（所有在棋盘上的泡）&lt;br/&gt;this.bubbles.forEach(function (row) {&lt;br/&gt;row.forEach(function (bubble) {&lt;br/&gt;bubble.draw();&lt;br/&gt;});&lt;br/&gt;});&lt;br/&gt;ctx.restore();&lt;br/&gt;},&lt;br/&gt;addBubble: function (bubble) {&lt;br/&gt;var thisBubble = this.bubbles[bubble.x][bubble.y];&lt;br/&gt;thisBubble.color = bubble.color;&lt;br/&gt;},&lt;br/&gt;getBubble: function (x, y) {&lt;br/&gt;var thisBubble = this.bubbles[x][y];&lt;br/&gt;if (!thisBubble.color) {&lt;br/&gt;return null;&lt;br/&gt;}&lt;br/&gt;else {&lt;br/&gt;return thisBubble;&lt;br/&gt;}&lt;br/&gt;}&lt;br/&gt;};&lt;br/&gt;&lt;/div&gt;&lt;p&gt;map的init初始化方法里我先把所有的泡泡部署好了，但是都没有染色，我并没有在ui的背后维护一个数组，因为我觉得泡泡有没有颜色就代表 0,1了，所以就这样也行。&lt;br /&gt;draw方法不再想之前那样把起始坐标计算进去了，而是使用了translate方法，这样就很方便写代码了。&lt;br /&gt;addBubble其实就是染色而已，接收参数是一个泡泡对象，这个对象来自ready区域的泡泡。&lt;/p&gt;&lt;p&gt;Ready区域其实就像俄罗斯方块那样，有三个预备的泡泡即将进入map区域。&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;game.ready = {&lt;br/&gt;startX: 40.5,&lt;br/&gt;startY: 20.5,&lt;br/&gt;width: game.cellWidth * 3,&lt;br/&gt;height: game.cellWidth,&lt;br/&gt;bubbles: [],&lt;br/&gt;init: function () {&lt;br/&gt;this.genrate();&lt;br/&gt;var me = this;&lt;br/&gt;me.flyin();&lt;br/&gt;},&lt;br/&gt;genrate: function () {&lt;br/&gt;for (var i = 0; i &amp;lt; 3; i++) {&lt;br/&gt;var color = game.colors[game.getRandom(game.mode)];&lt;br/&gt;this.bubbles.push(new Bubble(i, 0, color));&lt;br/&gt;}&lt;br/&gt;},&lt;br/&gt;draw: function () {&lt;br/&gt;var ctx = game.ctx;&lt;br/&gt;ctx.save();&lt;br/&gt;ctx.translate(this.startX, this.startY);&lt;br/&gt;ctx.beginPath();&lt;br/&gt;ctx.strokeStyle = "#555";&lt;br/&gt;ctx.strokeRect(0, 0, this.width, this.height);&lt;br/&gt;ctx.stroke();&lt;br/&gt;//绘制准备的泡&lt;br/&gt;this.bubbles.forEach(function (bubble) {&lt;br/&gt;bubble.draw();&lt;br/&gt;});&lt;br/&gt;&lt;br/&gt;ctx.restore();&lt;br/&gt;},&lt;br/&gt;};&lt;br/&gt;&lt;/div&gt;&lt;p&gt;ready.init 初始化3个泡泡，并且把这3个泡泡&amp;ldquo;飞入&amp;rdquo;到map里，ready.draw很简单就是绘制一个小矩形和3个泡泡。&lt;/p&gt;&lt;p&gt;哦，对了，我们的泡泡的绘制代码也稍作了修改，现在的样子不是之前的纯色了，有了水晶效果。。。不妨看看：&amp;nbsp;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;        Bubble.prototype.draw = function () {&lt;br/&gt;            if (!this.color) {&lt;br/&gt;                return;&lt;br/&gt;            }&lt;br/&gt;            var ctx = game.ctx;&lt;br/&gt;            ctx.beginPath();&lt;br/&gt;            //console.log("x:" + px + "y:" + py);&lt;br/&gt;            var gradient = ctx.createRadialGradient(this.px - 5, this.py - 5, 0, this.px, this.py, this.light);&lt;br/&gt;            gradient.addColorStop(0, "white");&lt;br/&gt;            gradient.addColorStop(1, this.color);&lt;br/&gt;            ctx.arc(this.px, this.py, 11, 0, Math.PI * 2);&lt;br/&gt;            ctx.strokeStyle = this.color;&lt;br/&gt;            ctx.fillStyle = gradient;&lt;br/&gt;            ctx.fill();&lt;br/&gt;            ctx.stroke();&lt;br/&gt;        };&lt;br/&gt;&lt;/div&gt;&lt;p&gt;　createRadialGradient方法是画一个放射性的圆，起始在左上角，这样就有个光照效果，还是不错的。 看下效果图吧&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/11157/2012031717213691.jpg" alt="" /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/mad/aggbug/2392632.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/mad/archive/2012/03/17/2392632.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/mad/archive/2012/03/11/2390281.html</id><title type="text">HTML5 Canvas编写五彩连珠（2）：画图</title><summary type="text">好吧，新的一天来了，我才开始动笔，真够懒得：）昨天说了今天我们要画一个球，在canvas上。好吧，这是游戏的入门的第一步，只是昨天没写完，所以。。。&lt;!DOCTYPE html&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;head&gt; &lt;title&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt; &lt;canvas id="canvas" height="400" width="600" st</summary><published>2012-03-11T08:36:00Z</published><updated>2012-03-11T08:36:00Z</updated><author><name>君之蘭</name><uri>http://www.cnblogs.com/mad/</uri></author><link rel="alternate" href="http://www.cnblogs.com/mad/archive/2012/03/11/2390281.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/mad/archive/2012/03/11/2390281.html"/><content type="html">&lt;p&gt;好吧，新的一天来了，我才开始动笔，真够懒得：）昨天说了今天我们要画一个球，在canvas上。好吧，这是游戏的入门的第一步，只是昨天没写完，所以。。。&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;br/&gt;&amp;lt;html xmlns="http://www.w3.org/1999/xhtml"&amp;gt;&lt;br/&gt;&amp;lt;head&amp;gt;&lt;br/&gt;    &amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;br/&gt;&amp;lt;/head&amp;gt;&lt;br/&gt;&amp;lt;body&amp;gt;&lt;br/&gt;    &amp;lt;canvas id="canvas" height="400" width="600" style="background: #fff;"&amp;gt;&amp;lt;/canvas&amp;gt;&lt;br/&gt;    &amp;lt;script type="text/javascript"&amp;gt;&lt;br/&gt;        var canvas = document.getElementById("canvas");&lt;br/&gt;        var ctx = canvas.getContext("2d");&lt;br/&gt;        ctx.beginPath();&lt;br/&gt;&lt;br/&gt;        ctx.arc(110, 110, 40, 0, Math.PI * 2);&lt;br/&gt;&lt;br/&gt;        ctx.strokeStyle = "red";&lt;br/&gt;        ctx.fillStyle = "yellow";&lt;br/&gt;&lt;br/&gt;        ctx.fill();&lt;br/&gt;        ctx.stroke();&lt;br/&gt;&lt;br/&gt;    &amp;lt;/script&amp;gt;&lt;br/&gt;&amp;lt;/body&amp;gt;&lt;br/&gt;&amp;lt;/html&amp;gt;&lt;br/&gt;&lt;/div&gt;&lt;p&gt;上面的代码是在VS11 beta上写的，实在是太舒服了，vs是非常强大的编辑器。上面的代码中我们绘制了一个大大的圆，并且着色了，红边和黄心。&lt;br /&gt;看下 arc (弧度)方法，昨天的文章里有他的链接地址，我在这里粘贴下。&lt;br /&gt;&amp;nbsp;&lt;span&gt;The&amp;nbsp;&lt;/span&gt;&lt;dfn id="dom-context-2d-arc" title="dom-context-2d-arc"&gt;&lt;code&gt;arc(&lt;var title=""&gt;x&lt;/var&gt;,&amp;nbsp;&lt;var title=""&gt;y&lt;/var&gt;,&amp;nbsp;&lt;var title=""&gt;radius&lt;/var&gt;,&amp;nbsp;&lt;var title=""&gt;startAngle&lt;/var&gt;,&amp;nbsp;&lt;var title=""&gt;endAngle&lt;/var&gt;,&amp;nbsp;&lt;var title=""&gt;anticlockwise&lt;/var&gt;)&lt;/code&gt;&lt;/dfn&gt;&lt;span&gt;&amp;nbsp;method draws an arc.&amp;nbsp;&lt;br /&gt;arc(x,y,弧度,开始角度点，结束角度点， 顺时针)，角度点你可能不是很清楚如何表示，这里是用Math.PI 圆周率来表示 6.28是周长。 想画圆的一部分也就是一段弧线，可以取开始的角度点和结束的角度点。&lt;br /&gt;&lt;br /&gt;&amp;nbsp;那么下一步就是要把圆能够画到我们棋盘上。其实，这个很简单，只要我们把x,y和radius的值调整下就会绘制出来。我把昨天代码写的更&amp;ldquo;专业&amp;rdquo;了一点。所以，今天的代码会在新的代码基础上增加了。先看下改动过的代码。&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;        var canvas = document.getElementById("canvas");&lt;br/&gt;        var ctx = canvas.getContext("2d");&lt;br/&gt;        var g = {&lt;br/&gt;            cellCount: 9,&lt;br/&gt;            lineCount: 5,&lt;br/&gt;        };&lt;br/&gt;&lt;br/&gt;        var map = {&lt;br/&gt;            startX: 20.5,&lt;br/&gt;            startY: 60.5,&lt;br/&gt;            cellWidth: 30,&lt;br/&gt;            getEndX: function () {&lt;br/&gt;                return g.cellCount * this.cellWidth + this.startX;&lt;br/&gt;            },&lt;br/&gt;            getEndY: function () {&lt;br/&gt;                return g.cellCount * this.cellWidth + this.startY;&lt;br/&gt;            },&lt;br/&gt;            draw: function () {&lt;br/&gt;                ctx.beginPath();&lt;br/&gt;&lt;br/&gt;                ctx.moveTo(this.startX, this.startY);&lt;br/&gt;&lt;br/&gt;                for (var i = 0; i &amp;lt;= g.cellCount; i++) {&lt;br/&gt;&lt;br/&gt;                    var p1 = i * this.cellWidth + this.startX;&lt;br/&gt;                    ctx.moveTo(p1, this.startY);&lt;br/&gt;                    ctx.lineTo(p1, this.getEndY());&lt;br/&gt;&lt;br/&gt;                    var p2 = i * this.cellWidth + this.startY;&lt;br/&gt;                    ctx.moveTo(this.startX, p2);&lt;br/&gt;                    ctx.lineTo(this.getEndX(), p2);&lt;br/&gt;&lt;br/&gt;                }&lt;br/&gt;                ctx.strokeStyle = "#456";&lt;br/&gt;                ctx.stroke();&lt;br/&gt;            },&lt;br/&gt;&lt;br/&gt;        };&lt;br/&gt;&lt;br/&gt;        map.draw();&lt;/div&gt;&lt;p&gt;是吧，更专业了吧，这样就不会定义一坨的function，到时候没出找，而是定义在一个对象里，这种封装也能避免命名冲突。而且，棋盘起始的位置我也做了调整，只要修改起始的x y值，棋盘就会在这个点开始画。 &amp;nbsp;那，现在我们要在第五行，第六列画一个黄色的球，该怎么画呢？只需要给map增加一个方法，再调用这个方法就行啦&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;            drawBubble: function (x, y) {&lt;br/&gt;                var px = this.startX + this.cellWidth * x - this.cellWidth / 2;&lt;br/&gt;                var py = this.startY + this.cellWidth * y - this.cellWidth / 2;&lt;br/&gt;                ctx.beginPath();&lt;br/&gt;                ctx.arc(px, py, 12, 0, Math.PI * 2);&lt;br/&gt;                ctx.strokeStyle = "white";&lt;br/&gt;                ctx.fillStyle = "yellow";&lt;br/&gt;                ctx.fill();&lt;br/&gt;                ctx.stroke();&lt;br/&gt;            },&lt;/div&gt;&lt;p&gt;画出来刷新下，居然是第六行，第五列，我们搞错了吗？代码没有，只是我们误认为x是行y是列 按顺序叫顺口了，其实是反过来的：）&lt;/p&gt;&lt;p&gt;泡泡既然能画出来，也要能清除才是，不然没法玩，所以再增加一个清除泡泡的方法。&lt;br /&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;            clearBubble: function (x, y) {&lt;br/&gt;                var px = this.startX + this.cellWidth * x - this.cellWidth + 0.5;&lt;br/&gt;                var py = this.startY + this.cellWidth * y - this.cellWidth + 0.5;&lt;br/&gt;                ctx.beginPath();&lt;br/&gt;                ctx.clearRect(px, py, this.cellWidth - 1, this.cellWidth - 1);&lt;br/&gt;                ctx.stroke();&lt;br/&gt;            }&lt;br/&gt;&lt;/div&gt;&lt;p&gt;　ok，是不是很霸气？ o(&amp;cap;_&amp;cap;)o 哈哈，不过在获取泡泡的位置时是不是很纠结，为什么画泡泡是 width/2 而擦除要加0.5？&amp;nbsp;&lt;br /&gt;画圆是从中心点开始画，所以要去掉半径，而擦数不能擦掉我们的棋盘线，所以要偏移0.5 。&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/11157/2012031410464515.gif" alt="" /&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/mad/aggbug/2390281.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/mad/archive/2012/03/11/2390281.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/mad/archive/2012/03/10/2389519.html</id><title type="text">HTML5 Canvas编写五彩连珠（1）：预览</title><summary type="text">HTML5推出也有很长一段时间了，一直没有学习过，闲来无事学习开发个游戏吧。 用javascript+canvas编写一个 五彩连珠的游戏。Canvas 画布 标签&lt;canvas id="canvas" &gt;&lt;/canvas&gt;，很简单和普通的tag没区别。 关键在于js对他的操作。先看个示例代码：&lt;canvas id="canvas" height="100" width="100"&gt;&lt;/canvas&gt;&lt;script&gt;var canvas = docume</summary><published>2012-03-10T13:28:00Z</published><updated>2012-03-10T13:28:00Z</updated><author><name>君之蘭</name><uri>http://www.cnblogs.com/mad/</uri></author><link rel="alternate" href="http://www.cnblogs.com/mad/archive/2012/03/10/2389519.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/mad/archive/2012/03/10/2389519.html"/><content type="html">&lt;p&gt;HTML5推出也有很长一段时间了，一直没有学习过，闲来无事学习开发个游戏吧。 &amp;nbsp;用javascript+canvas编写一个 五彩连珠的游戏。&lt;/p&gt;&lt;p&gt;Canvas 画布&lt;br /&gt; 标签&amp;lt;canvas id="canvas" &amp;gt;&amp;lt;/canvas&amp;gt;，很简单和普通的tag没区别。 关键在于js对他的操作。先看个示例代码：&lt;/p&gt;&amp;lt;canvas id="canvas" height="100" width="100"&amp;gt;&amp;lt;/canvas&amp;gt;&lt;br/&gt;&amp;lt;script&amp;gt;&lt;br/&gt;var canvas = document.getElementById("canvas");&lt;br/&gt;var ctx  = canvas.getContext("2d");&lt;br/&gt;ctx.beginPath();&lt;br/&gt;ctx.moveTo(50,10);&lt;br/&gt;ctx.lineTo(50,90);&lt;br/&gt;ctx.moveTo(10,50);&lt;br/&gt;ctx.lineTo(90,50);&lt;br/&gt;&lt;br/&gt;ctx.strokeStyle="red";&lt;br/&gt;ctx.stroke();&lt;br/&gt;&amp;lt;/script&amp;gt;&lt;p&gt;你能看到想到我画的是什么吗？ &amp;nbsp;ctx是canvas的绘制的类型2D的，以后会支持3D，那木，目前基于canvas的绘制都是调用2d context的方法。所以要了解绘制各种图形，得先看看他的api。&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;interface &lt;dfn id="canvasrenderingcontext2d" style="font-weight: bold; font-style: normal;"&gt;CanvasRenderingContext2D&lt;/dfn&gt; {&lt;br/&gt;&lt;br/&gt;  // back-reference to the canvas&lt;br/&gt;  readonly attribute HTMLCanvasElement &lt;a style="background-color: transparent;" title="dom-context-2d-canvas" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-canvas"&gt;canvas&lt;/a&gt;;&lt;br/&gt;&lt;br/&gt;  // state&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-save" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-save"&gt;save&lt;/a&gt;(); // push state on state stack&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-restore" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-restore"&gt;restore&lt;/a&gt;(); // pop state stack and restore state&lt;br/&gt;  // transformations (default transform is the identity matrix)&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-scale" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-scale"&gt;scale&lt;/a&gt;(in double x, in double y);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-rotate" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-rotate"&gt;rotate&lt;/a&gt;(in double angle);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-translate" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-translate"&gt;translate&lt;/a&gt;(in double x, in double y);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-transform" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-transform"&gt;transform&lt;/a&gt;(in double a, in double b, in double c, in double d, in double e, in double f);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-setTransform" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-settransform"&gt;setTransform&lt;/a&gt;(in double a, in double b, in double c, in double d, in double e, in double f);&lt;br/&gt;  // compositing&lt;br/&gt;           attribute double &lt;a style="background-color: transparent;" title="dom-context-2d-globalAlpha" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-globalalpha"&gt;globalAlpha&lt;/a&gt;; // (default 1.0)&lt;br/&gt;           attribute DOMString &lt;a style="background-color: transparent;" title="dom-context-2d-globalCompositeOperation" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-globalcompositeoperation"&gt;globalCompositeOperation&lt;/a&gt;; // (default source-over)&lt;br/&gt;  // colors and styles&lt;br/&gt;           attribute any &lt;a style="background-color: transparent;" title="dom-context-2d-strokeStyle" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-strokestyle"&gt;strokeStyle&lt;/a&gt;; // (default black)&lt;br/&gt;           attribute any &lt;a style="background-color: transparent;" title="dom-context-2d-fillStyle" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-fillstyle"&gt;fillStyle&lt;/a&gt;; // (default black)&lt;br/&gt;  &lt;a style="background-color: transparent;" href="http://www.w3.org/TR/2dcontext/#canvasgradient"&gt;CanvasGradient&lt;/a&gt; &lt;a style="background-color: transparent;" title="dom-context-2d-createLinearGradient" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-createlineargradient"&gt;createLinearGradient&lt;/a&gt;(in double x0, in double y0, in double x1, in double y1);&lt;br/&gt;  &lt;a style="background-color: transparent;" href="http://www.w3.org/TR/2dcontext/#canvasgradient"&gt;CanvasGradient&lt;/a&gt; &lt;a style="background-color: transparent;" title="dom-context-2d-createRadialGradient" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-createradialgradient"&gt;createRadialGradient&lt;/a&gt;(in double x0, in double y0, in double r0, in double x1, in double y1, in double r1);&lt;br/&gt;  &lt;a style="background-color: transparent;" href="http://www.w3.org/TR/2dcontext/#canvaspattern"&gt;CanvasPattern&lt;/a&gt; &lt;a style="background-color: transparent;" title="dom-context-2d-createPattern" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-createpattern"&gt;createPattern&lt;/a&gt;(in HTMLImageElement image, in DOMString repetition);&lt;br/&gt;  &lt;a style="background-color: transparent;" href="http://www.w3.org/TR/2dcontext/#canvaspattern"&gt;CanvasPattern&lt;/a&gt; &lt;a style="background-color: transparent;" title="dom-context-2d-createPattern" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-createpattern"&gt;createPattern&lt;/a&gt;(in HTMLCanvasElement image, in DOMString repetition);&lt;br/&gt;  &lt;a style="background-color: transparent;" href="http://www.w3.org/TR/2dcontext/#canvaspattern"&gt;CanvasPattern&lt;/a&gt; &lt;a style="background-color: transparent;" title="dom-context-2d-createPattern" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-createpattern"&gt;createPattern&lt;/a&gt;(in HTMLVideoElement image, in DOMString repetition);&lt;br/&gt;&lt;br/&gt;  // line caps/joins&lt;br/&gt;           attribute double &lt;a style="background-color: transparent;" title="dom-context-2d-lineWidth" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-linewidth"&gt;lineWidth&lt;/a&gt;; // (default 1)&lt;br/&gt;           attribute DOMString &lt;a style="background-color: transparent;" title="dom-context-2d-lineCap" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-linecap"&gt;lineCap&lt;/a&gt;; // "butt", "round", "square" (default "butt")&lt;br/&gt;           attribute DOMString &lt;a style="background-color: transparent;" title="dom-context-2d-lineJoin" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-linejoin"&gt;lineJoin&lt;/a&gt;; // "round", "bevel", "miter" (default "miter")&lt;br/&gt;           attribute double &lt;a style="background-color: transparent;" title="dom-context-2d-miterLimit" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-miterlimit"&gt;miterLimit&lt;/a&gt;; // (default 10)&lt;br/&gt;&lt;br/&gt;  // shadows&lt;br/&gt;           attribute double &lt;a style="background-color: transparent;" title="dom-context-2d-shadowOffsetX" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-shadowoffsetx"&gt;shadowOffsetX&lt;/a&gt;; // (default 0)&lt;br/&gt;           attribute double &lt;a style="background-color: transparent;" title="dom-context-2d-shadowOffsetY" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-shadowoffsety"&gt;shadowOffsetY&lt;/a&gt;; // (default 0)&lt;br/&gt;           attribute double &lt;a style="background-color: transparent;" title="dom-context-2d-shadowBlur" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-shadowblur"&gt;shadowBlur&lt;/a&gt;; // (default 0)&lt;br/&gt;           attribute DOMString &lt;a style="background-color: transparent;" title="dom-context-2d-shadowColor" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-shadowcolor"&gt;shadowColor&lt;/a&gt;; // (default transparent black)&lt;br/&gt;&lt;br/&gt;  // rects&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-clearRect" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-clearrect"&gt;clearRect&lt;/a&gt;(in double x, in double y, in double w, in double h);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-fillRect" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-fillrect"&gt;fillRect&lt;/a&gt;(in double x, in double y, in double w, in double h);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-strokeRect" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-strokerect"&gt;strokeRect&lt;/a&gt;(in double x, in double y, in double w, in double h);&lt;br/&gt;&lt;br/&gt;  // path API&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-beginPath" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-beginpath"&gt;beginPath&lt;/a&gt;();&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-closePath" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-closepath"&gt;closePath&lt;/a&gt;();&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-moveTo" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-moveto"&gt;moveTo&lt;/a&gt;(in double x, in double y);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-lineTo" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-lineto"&gt;lineTo&lt;/a&gt;(in double x, in double y);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-quadraticCurveTo" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-quadraticcurveto"&gt;quadraticCurveTo&lt;/a&gt;(in double cpx, in double cpy, in double x, in double y);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-bezierCurveTo" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-beziercurveto"&gt;bezierCurveTo&lt;/a&gt;(in double cp1x, in double cp1y, in double cp2x, in double cp2y, in double x, in double y);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-arcTo" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-arcto"&gt;arcTo&lt;/a&gt;(in double x1, in double y1, in double x2, in double y2, in double radius);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-rect" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-rect"&gt;rect&lt;/a&gt;(in double x, in double y, in double w, in double h);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-arc" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-arc"&gt;arc&lt;/a&gt;(in double x, in double y, in double radius, in double startAngle, in double endAngle, in optional boolean anticlockwise);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-fill" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-fill"&gt;fill&lt;/a&gt;();&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-stroke" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-stroke"&gt;stroke&lt;/a&gt;();&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-clip" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-clip"&gt;clip&lt;/a&gt;();&lt;br/&gt;  boolean &lt;a style="background-color: transparent;" title="dom-context-2d-isPointInPath" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-ispointinpath"&gt;isPointInPath&lt;/a&gt;(in double x, in double y);&lt;br/&gt;&lt;br/&gt;  // Focus management&lt;br/&gt;  boolean &lt;a style="background-color: transparent;" title="dom-context-2d-drawFocusRing" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-drawfocusring"&gt;drawFocusRing&lt;/a&gt;(in Element element, in optional boolean canDrawCustom);&lt;br/&gt;&lt;br/&gt;  // Caret and selection management&lt;br/&gt;  long &lt;a id="dom-context-2d-caretBlinkRate" style="background-color: transparent;" title="dom-context-2d-caretBlinkRate" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-caretBlinkRate"&gt;caretBlinkRate&lt;/a&gt;();&lt;br/&gt;  boolean &lt;a style="background-color: transparent;" title="dom-context-2d-setCaretSelectionRect" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-setCaretSelectionRect"&gt;setCaretSelectionRect&lt;/a&gt;(in Element element, in double x, in double y, in double w, in double h);&lt;br/&gt;&lt;br/&gt;  // text&lt;br/&gt;           attribute DOMString &lt;a style="background-color: transparent;" title="dom-context-2d-font" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-font"&gt;font&lt;/a&gt;; // (default 10px sans-serif)&lt;br/&gt;           attribute DOMString &lt;a style="background-color: transparent;" title="dom-context-2d-textAlign" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-textalign"&gt;textAlign&lt;/a&gt;; // "start", "end", "left", "right", "center" (default: "start")&lt;br/&gt;           attribute DOMString &lt;a style="background-color: transparent;" title="dom-context-2d-textBaseline" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-textbaseline"&gt;textBaseline&lt;/a&gt;; // "top", "hanging", "middle", "alphabetic", "ideographic", "bottom" (default: "alphabetic")&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-fillText" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-filltext"&gt;fillText&lt;/a&gt;(in DOMString text, in double x, in double y, in optional double maxWidth);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-strokeText" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-stroketext"&gt;strokeText&lt;/a&gt;(in DOMString text, in double x, in double y, in optional double maxWidth);&lt;br/&gt;  &lt;a style="background-color: transparent;" href="http://www.w3.org/TR/2dcontext/#textmetrics"&gt;TextMetrics&lt;/a&gt; &lt;a style="background-color: transparent;" title="dom-context-2d-measureText" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-measuretext"&gt;measureText&lt;/a&gt;(in DOMString text);&lt;br/&gt;&lt;br/&gt;  // drawing images&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-drawImage" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-drawimage"&gt;drawImage&lt;/a&gt;(in HTMLImageElement image, in double dx, in double dy, in optional double dw, in double dh);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-drawImage" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-drawimage"&gt;drawImage&lt;/a&gt;(in HTMLImageElement image, in double sx, in double sy, in double sw, in double sh, in double dx, in double dy, in double dw, in double dh);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-drawImage" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-drawimage"&gt;drawImage&lt;/a&gt;(in HTMLCanvasElement image, in double dx, in double dy, in optional double dw, in double dh);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-drawImage" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-drawimage"&gt;drawImage&lt;/a&gt;(in HTMLCanvasElement image, in double sx, in double sy, in double sw, in double sh, in double dx, in double dy, in double dw, in double dh);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-drawImage" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-drawimage"&gt;drawImage&lt;/a&gt;(in HTMLVideoElement image, in double dx, in double dy, in optional double dw, in double dh);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-drawImage" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-drawimage"&gt;drawImage&lt;/a&gt;(in HTMLVideoElement image, in double sx, in double sy, in double sw, in double sh, in double dx, in double dy, in double dw, in double dh);&lt;br/&gt;&lt;br/&gt;  // pixel manipulation&lt;br/&gt;  &lt;a style="background-color: transparent;" href="http://www.w3.org/TR/2dcontext/#imagedata"&gt;ImageData&lt;/a&gt; &lt;a style="background-color: transparent;" title="dom-context-2d-createImageData" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-createimagedata"&gt;createImageData&lt;/a&gt;(in double sw, in double sh);&lt;br/&gt;  &lt;a style="background-color: transparent;" href="http://www.w3.org/TR/2dcontext/#imagedata"&gt;ImageData&lt;/a&gt; &lt;a style="background-color: transparent;" title="dom-context-2d-createImageData" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-createimagedata"&gt;createImageData&lt;/a&gt;(in &lt;a style="background-color: transparent;" href="http://www.w3.org/TR/2dcontext/#imagedata"&gt;ImageData&lt;/a&gt; imagedata);&lt;br/&gt;  &lt;a style="background-color: transparent;" href="http://www.w3.org/TR/2dcontext/#imagedata"&gt;ImageData&lt;/a&gt; &lt;a style="background-color: transparent;" title="dom-context-2d-getImageData" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-getimagedata"&gt;getImageData&lt;/a&gt;(in double sx, in double sy, in double sw, in double sh);&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-context-2d-putImageData" href="http://www.w3.org/TR/2dcontext/#dom-context-2d-putimagedata"&gt;putImageData&lt;/a&gt;(in &lt;a style="background-color: transparent;" href="http://www.w3.org/TR/2dcontext/#imagedata"&gt;ImageData&lt;/a&gt; imagedata, in double dx, in double dy, in optional double dirtyX, in double dirtyY, in double dirtyWidth, in double dirtyHeight);&lt;br/&gt;};&lt;br/&gt;&lt;br/&gt;interface &lt;dfn id="canvasgradient" style="font-weight: bold; font-style: normal;"&gt;CanvasGradient&lt;/dfn&gt; {&lt;br/&gt;  // opaque object&lt;br/&gt;  void &lt;a style="background-color: transparent;" title="dom-canvasgradient-addColorStop" href="http://www.w3.org/TR/2dcontext/#dom-canvasgradient-addcolorstop"&gt;addColorStop&lt;/a&gt;(in double offset, in DOMString color);&lt;br/&gt;};&lt;br/&gt;&lt;br/&gt;interface &lt;dfn id="canvaspattern" style="font-weight: bold; font-style: normal;"&gt;CanvasPattern&lt;/dfn&gt; {&lt;br/&gt;  // opaque object&lt;br/&gt;};&lt;br/&gt;&lt;br/&gt;interface &lt;dfn id="textmetrics" style="font-weight: bold; font-style: normal;"&gt;TextMetrics&lt;/dfn&gt; {&lt;br/&gt;  readonly attribute double &lt;a style="background-color: transparent;" title="dom-textmetrics-width" href="http://www.w3.org/TR/2dcontext/#dom-textmetrics-width"&gt;width&lt;/a&gt;;&lt;br/&gt;};&lt;br/&gt;&lt;br/&gt;interface &lt;dfn id="imagedata" style="font-weight: bold; font-style: normal;"&gt;ImageData&lt;/dfn&gt; {&lt;br/&gt;  readonly attribute unsigned long &lt;a style="background-color: transparent;" title="dom-imagedata-width" href="http://www.w3.org/TR/2dcontext/#dom-imagedata-width"&gt;width&lt;/a&gt;;&lt;br/&gt;  readonly attribute unsigned long &lt;a style="background-color: transparent;" title="dom-imagedata-height" href="http://www.w3.org/TR/2dcontext/#dom-imagedata-height"&gt;height&lt;/a&gt;;&lt;br/&gt;  readonly attribute &lt;a style="background-color: transparent;" href="http://www.w3.org/TR/2dcontext/#canvaspixelarray"&gt;CanvasPixelArray&lt;/a&gt; &lt;a style="background-color: transparent;" title="dom-imagedata-data" href="http://www.w3.org/TR/2dcontext/#dom-imagedata-data"&gt;data&lt;/a&gt;;&lt;br/&gt;};&lt;br/&gt;&lt;br/&gt;interface &lt;dfn id="canvaspixelarray" style="font-weight: bold; font-style: normal;"&gt;CanvasPixelArray&lt;/dfn&gt; {&lt;br/&gt;  readonly attribute unsigned long &lt;a style="background-color: transparent;" title="dom-canvaspixelarray-length" href="http://www.w3.org/TR/2dcontext/#dom-canvaspixelarray-length"&gt;length&lt;/a&gt;;&lt;br/&gt;  &lt;a style="background-color: transparent;" title="dom-CanvasPixelArray-get" href="http://www.w3.org/TR/2dcontext/#dom-canvaspixelarray-get"&gt;getter&lt;/a&gt; octet (in unsigned long index);&lt;br/&gt;  &lt;a style="background-color: transparent;" title="dom-CanvasPixelArray-set" href="http://www.w3.org/TR/2dcontext/#dom-canvaspixelarray-set"&gt;setter&lt;/a&gt; void (in unsigned long index, in octet value);&lt;br/&gt;};&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;上面的内容是我粘贴的官方的，一目了然。&amp;nbsp;&lt;/p&gt;&lt;p&gt;既然我们知道了lineTo和moveTo的功能，那么我们先把游戏的格子棋盘先画出来：&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&amp;lt;canvas id="canvas" height="600" width="780" style="border:solid 1px #369;background:#333"&amp;gt;&amp;lt;/canvas&amp;gt;&lt;br/&gt;&amp;lt;script&amp;gt;&lt;br/&gt;var canvas = document.getElementById("canvas");&lt;br/&gt;&lt;br/&gt;var ctx  = canvas.getContext("2d");&lt;br/&gt;&lt;br/&gt;drawMap();&lt;br/&gt;&lt;br/&gt;function drawMap()&lt;br/&gt;{&lt;br/&gt;var start = 10;&lt;br/&gt;ctx.beginPath();&lt;br/&gt;var cell = 30;&lt;br/&gt;var max = cell * 9 + start;&lt;br/&gt;//ctx.strokeRect(10,10,max,max);&lt;br/&gt;ctx.moveTo(start,start);&lt;br/&gt;&lt;br/&gt;for(var i = 0;i &amp;lt;= 9 ;i++){&lt;br/&gt;var p = i * cell + start + 0.5;&lt;br/&gt;ctx.lineTo(p,max);&lt;br/&gt;ctx.moveTo(p+cell,start);&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;for(var i = 0;i &amp;lt;= 9 ;i++){&lt;br/&gt;var p = i * cell + start + 0.5;&lt;br/&gt;ctx.moveTo(start,p);&lt;br/&gt;ctx.lineTo(max,p);&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;ctx.strokeStyle="#567";&lt;br/&gt;ctx.stroke();&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;&amp;lt;/script&amp;gt;&lt;p&gt;&lt;br /&gt;从运行效果可以看到我们的棋盘是从10像素的位置开始画的，画了个9*9格子的五彩连珠棋盘。&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/11157/2012031410474192.gif" alt="" /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;今天入门就到这里，下一节讲怎么画一个球。。。&lt;/p&gt;&lt;img src="http://www.cnblogs.com/mad/aggbug/2389519.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/mad/archive/2012/03/10/2389519.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/mad/archive/2012/03/10/2389513.html</id><title type="text">.NET 4.0的dynamic特性和协变特性应用一例</title><summary type="text">今天遇到了一个编码问题，很有意思如果T1,T2,T3都有相同的属性，而且处理这些属性的逻辑都一样，但他们之间没有关系，该怎么办？答案如下：namespace ConsoleApplication2 { public class T1 { public string Name { get; set; } public int Count { get; set; } } public class T2 { public string Name { get; set; } public int Count { get; set; } } public class T3...</summary><published>2012-03-10T13:21:00Z</published><updated>2012-03-10T13:21:00Z</updated><author><name>君之蘭</name><uri>http://www.cnblogs.com/mad/</uri></author><link rel="alternate" href="http://www.cnblogs.com/mad/archive/2012/03/10/2389513.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/mad/archive/2012/03/10/2389513.html"/><content type="html">&lt;div&gt;今天遇到了一个编码问题，很有意思&lt;br /&gt;如果T1,T2,T3&amp;nbsp;都有相同的属性，而且处理这些属性的逻辑都一样，但&lt;strong&gt;他们之间没有关&lt;/strong&gt;系，该怎么办？&lt;/div&gt;&lt;div&gt;答案如下：&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;namespace ConsoleApplication2  &lt;br/&gt;{  &lt;br/&gt;    public class T1 { public string Name { get; set; } public int Count { get; set; } }  &lt;br/&gt;    public class T2 { public string Name { get; set; } public int Count { get; set; } }  &lt;br/&gt;    public class T3 { public string Name { get; set; } public int Count { get; set; } }  &lt;br/&gt;    public class Class1  &lt;br/&gt;    {  &lt;br/&gt;        public void PrintName(dynamic t)  &lt;br/&gt;        {  &lt;br/&gt;            Console.WriteLine(t.Name);  &lt;br/&gt;        }  &lt;br/&gt;  &lt;br/&gt;        public void PrintCount(IEnumerable&amp;lt;dynamic&amp;gt; list)  &lt;br/&gt;        {  &lt;br/&gt;            Console.WriteLine(list.Sum(t =&amp;gt; t.Count));  &lt;br/&gt;        }  &lt;br/&gt;  &lt;br/&gt;        public void Test()  &lt;br/&gt;        {  &lt;br/&gt;            var t1 = new T1 { Name = "T1", Count = 1 };  &lt;br/&gt;            var t2 = new T2 { Name = "T2", Count = 2 };  &lt;br/&gt;            var t3 = new T3 { Name = "T3", Count = 3 };  &lt;br/&gt;  &lt;br/&gt;            PrintName(t1);  &lt;br/&gt;            PrintName(t2);  &lt;br/&gt;            PrintName(t3);  &lt;br/&gt;  &lt;br/&gt;            var list1 = new List&amp;lt;T1&amp;gt; { t1 };  &lt;br/&gt;            var list2 = new List&amp;lt;T2&amp;gt; { t2 };  &lt;br/&gt;            var list3 = new List&amp;lt;T3&amp;gt; { t3 };  &lt;br/&gt;  &lt;br/&gt;            PrintCount(list1);  &lt;br/&gt;            PrintCount(list2);  &lt;br/&gt;            PrintCount(list3);  &lt;br/&gt;        }  &lt;br/&gt;  &lt;br/&gt;    }  &lt;br/&gt;} &lt;br/&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;&lt;img src="http://www.cnblogs.com/mad/aggbug/2389513.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/mad/archive/2012/03/10/2389513.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/mad/archive/2011/11/25/trietree_filter_blockwords.html</id><title type="text">Trie树-脏词过滤应用</title><summary type="text">Trie树，又称字符查找树、前缀树，主要用于字符匹配（详见http://en.wikipedia.org/wiki/Trie）。适合做关键词查找，比如查找文章中的关键字然后给他们加链接。 当然对脏词的过滤应用也是样，只是把替换连接的工作换成了替换字符。当前的代码还只是进行简单的替换，并没有做一些字符的处理，比如“昨天见到你妈，逼我要买房”，这本身不是脏词，因为有逗号，所以程序里要增加字符的范围判断。程序中的skip就是用来过滤脏词的简单变体，比如“找*小*姐”，默认是最多跳过3个字符，这个可以随便调整了。总之是一个Trie的锻炼吧。 public class TrieTree { ...</summary><published>2011-11-25T05:45:00Z</published><updated>2011-11-25T05:45:00Z</updated><author><name>君之蘭</name><uri>http://www.cnblogs.com/mad/</uri></author><link rel="alternate" href="http://www.cnblogs.com/mad/archive/2011/11/25/trietree_filter_blockwords.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/mad/archive/2011/11/25/trietree_filter_blockwords.html"/><content type="html">&lt;p&gt;Trie树，又称字符查找树、前缀树，主要用于字符匹配（详见&lt;a href="http://en.wikipedia.org/wiki/Trie"&gt;http://en.wikipedia.org/wiki/Trie&lt;/a&gt;）。适合做关键词查找，比如查找文章中的关键字然后给他们加链接。 当然对脏词的过滤应用也是样，只是把替换连接的工作换成了替换字符。&lt;/p&gt;&lt;p&gt;当前的代码还只是进行简单的替换，并没有做一些字符的处理，比如&amp;ldquo;昨天见到你妈，逼我要买房&amp;rdquo;，这本身不是脏词，因为有逗号，所以程序里要增加字符的范围判断。&lt;/p&gt;&lt;p&gt;程序中的skip就是用来过滤脏词的简单变体，比如&amp;ldquo;找*小*姐&amp;rdquo;，默认是最多跳过3个字符，这个可以随便调整了。总之是一个Trie的锻炼吧。&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;    public class TrieTree&lt;br/&gt;    {&lt;br/&gt;        private readonly Dictionary&amp;lt;char, TrieTree&amp;gt; Children;&lt;br/&gt;&lt;br/&gt;        public bool End { get; set; }&lt;br/&gt;&lt;br/&gt;        public TrieTree()&lt;br/&gt;        {&lt;br/&gt;            Children = new Dictionary&amp;lt;char, TrieTree&amp;gt;();&lt;br/&gt;        }&lt;br/&gt;&lt;br/&gt;        public void AddKey(string keyword)&lt;br/&gt;        {&lt;br/&gt;            if (String.IsNullOrEmpty(keyword))&lt;br/&gt;            {&lt;br/&gt;                return;&lt;br/&gt;            }&lt;br/&gt;&lt;br/&gt;            var cNode = this;&lt;br/&gt;&lt;br/&gt;            foreach (var key in keyword)&lt;br/&gt;            {&lt;br/&gt;                if (cNode.Children.ContainsKey(key))&lt;br/&gt;                {&lt;br/&gt;                    cNode = cNode.Children[key];&lt;br/&gt;                }&lt;br/&gt;                else&lt;br/&gt;                {&lt;br/&gt;                    var node = new TrieTree();&lt;br/&gt;                    cNode.Children.Add(key, node);&lt;br/&gt;                    cNode = node;&lt;br/&gt;                }&lt;br/&gt;            }&lt;br/&gt;            cNode.End = true;&lt;br/&gt;        }&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;        public void Replace(ref string text)&lt;br/&gt;        {&lt;br/&gt;            for (var i = 0; i &amp;lt; text.Length; i++)&lt;br/&gt;            {&lt;br/&gt;                var cNode = this;&lt;br/&gt;                var key = text[i];&lt;br/&gt;                //碰到脏词的第一个词&lt;br/&gt;                if (cNode.Children.ContainsKey(key))&lt;br/&gt;                {&lt;br/&gt;                    cNode = cNode.Children[key];&lt;br/&gt;                    //查找是否包含脏词后面的词&lt;br/&gt;                    var skip = 0;&lt;br/&gt;                    for (var j = i + 1; j &amp;lt; text.Length; j++)&lt;br/&gt;                    {&lt;br/&gt;                        if (cNode.Children.ContainsKey(text[j]))&lt;br/&gt;                        {&lt;br/&gt;                            cNode = cNode.Children[text[j]];&lt;br/&gt;                            skip = 0;&lt;br/&gt;                        }&lt;br/&gt;                        else&lt;br/&gt;                        {&lt;br/&gt;                            //允许略过过几个字符&lt;br/&gt;                            skip++;&lt;br/&gt;                            if (skip &amp;gt; 3)&lt;br/&gt;                            {&lt;br/&gt;                                break;&lt;br/&gt;                            }&lt;br/&gt;                        }&lt;br/&gt;                        if (cNode.End)&lt;br/&gt;                        {&lt;br/&gt;                            var len = j + 1 - i;&lt;br/&gt;                            text = text.Replace(text.Substring(i, len), string.Empty.PadLeft(len, '*'));&lt;br/&gt;                            i += len;&lt;br/&gt;                            break;&lt;br/&gt;                        }&lt;br/&gt;                    }&lt;br/&gt;                }&lt;br/&gt;            }&lt;br/&gt;        }&lt;br/&gt;&lt;br/&gt;    }&lt;br/&gt;&lt;/div&gt;&lt;p&gt;使用方法如下：&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;    class Program&lt;br/&gt;    {&lt;br/&gt;        static void Main(string[] args)&lt;br/&gt;        {&lt;br/&gt;            var trie = new TrieTree();&lt;br/&gt;            var keywords = "我操,妓女,fuck".Split(',');&lt;br/&gt;            foreach (var key in keywords)&lt;br/&gt;            {&lt;br/&gt;                trie.AddKey(key);&lt;br/&gt;            }&lt;br/&gt;&lt;br/&gt;            var text = @"我擦啊，尼玛，我操 你 妈，fuck you，你这个妓女，贱人。";&lt;br/&gt;            trie.Replace(ref text);&lt;br/&gt;            Console.WriteLine(text);&lt;br/&gt;            Console.Read();&lt;br/&gt;        }&lt;br/&gt;    }&lt;br/&gt;&lt;/div&gt;&lt;p&gt;执行结果如下：&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2011/11157/2011112513544817.jpg" alt="" /&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/mad/aggbug/2263130.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/mad/archive/2011/11/25/trietree_filter_blockwords.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/mad/archive/2011/11/01/2232213.html</id><title type="text">带你走进缓存世界（6）：共享缓存</title><summary type="text">回顾之前的章节，我们大致讲了下缓存的基本概念、原理、策略和常用方法。可能会有朋友会说，访问量小根本不需要缓存，徒增开发的复杂度，访问量大的话本地缓存也没意义。其实这话说的也不无道理，当然我们不能把这句话看成绝对的话，不然什么话都成废话了：），其实我们研究学习的任何技术都不会白学的，每种技术都有他的使用范畴，只是当我们面对新的层次时，需要改变，需要重新学习。我们之前讲的每篇都非常有用，本地缓存也非常有用。不过当我们面临大的数据量和访问量的考研时，就需要使用新的解决方案。今天就讲讲共享缓存那些事。到底什么是共享缓存？为什么要用共享缓存？新的层次就是指当我们仅仅依赖数据库和本地缓存已经无法满足我们的</summary><published>2011-11-01T13:16:00Z</published><updated>2011-11-01T13:16:00Z</updated><author><name>君之蘭</name><uri>http://www.cnblogs.com/mad/</uri></author><link rel="alternate" href="http://www.cnblogs.com/mad/archive/2011/11/01/2232213.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/mad/archive/2011/11/01/2232213.html"/><content type="html">&lt;p&gt;&lt;span&gt;回顾之前的章节，我们大致讲了下缓存的基本概念、原理、策略和常用方法。可能会有朋友会说，访问量小根本不需要缓存，徒增开发的复杂度，访问量大的话本地缓存也没意义。其实这话说的也不无道理，当然我们不能把这句话看成绝对的话，不然什么话都成废话了：），其实我们研究学习的任何技术都不会白学的，每种技术都有他的使用范畴，只是当我们面对新的层次时，需要改变，需要重新学习。我们之前讲的每篇都非常有用，本地缓存也非常有用。不过当我们面临大的数据量和访问量的考研时，就需要使用新的解决方案。今天就讲讲共享缓存那些事。&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;到底什么是共享缓存？为什么要用共享缓存？&lt;br /&gt;&lt;/strong&gt;&lt;span&gt;新的层次就是指当我们仅仅依赖数据库和本地缓存已经无法满足我们的访问量时，我们该怎么处理？&lt;/span&gt;&lt;br /&gt;&lt;span&gt;其实问题突出在IO(Input/Output)上，就是说我们的IO能力不足了，解决办法其实很简单就是增加内存。但是，既然是增加内存为什么还谈公共缓存呢？其实这里牵扯到了一个分布式的整体部署问题。Web站点服务器一般不是IO为主的机器，而数据库服务器才是IO密集型服务器。如果我们只是在Web服务器上增加内存，虽然可以解决一时的IO紧张问题，但是Web服务一旦重启，缓存被清空，那么所有的请求都将会去数据库服务器请求，那么数据库直接就会垮掉，也就是说网站直接垮掉。这是其一，其二web服务器是CPU密集型服务器（也就是主要用于计算，包含数据的对象处理、压缩等），如果一台计算顶不住的时候或者连接数顶不住的时候，势必要增加web服务器，到时候缓存该建立在哪台服务器上呢？所以最理想的办法就是把缓存独立出来，这就是共享缓存服务器。&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;共享缓存和我们本地缓存的区别在哪里呢？&lt;br /&gt;&lt;/strong&gt;&lt;span&gt;最大的区别就是不在一台机器上，就是不在一个进程里都不好搞，你说是不。 所以要通过网络传输获取数据，但这要比本地缓存慢了不知多少倍，因为机器之间的通信是毫秒级的，而本地内存的数据处理是纳米级的！传输完了还不算，传输的对象也不是我们直接使用的数据类型，我们还要经过反序列化成我们想要的对象再进行处理。所以，共享缓存看起来貌似坏处多多。虽然慢，但是这也是无奈之举，因为我们不可能无限制的提升一台机器的能力，而且这种提升也是昂贵的，不如多台廉价机器并行计算；况且，再不济也比通过网络访问数据库快吧:)，数据库可是从磁盘扫描的啊，这个速度也是毫秒级的。&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;怎么开发共享缓存呢？&lt;br /&gt;&lt;/strong&gt;&lt;span&gt;既然不是一台机器，那么共享缓存就牵扯到了网络通信技术，我们平时使用的通信协议就是tcp、http，而.net自带的通信框架就是WCF，还有就是WebService，你是不是有种WebService太不靠谱的感觉？Http是无状态协议，不能保持连接，每次都要重新建立打开关闭，这并不太适合我们，而采用类似数据库的连接池方式最合适（反过来想，我们开发的何尝不就是数据库？只不过是key/value的内存数据库罢了）所以选用WCF，使用TCP协议更适合我们，只是我们需要在以前的代码基础上增加网络通信和序列化、压缩等代码:)，再复杂的话那就是安全、分布式支持咯。&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;我们需要开发共享缓存吗？&lt;br /&gt;&lt;/strong&gt;&lt;span&gt;看到WCF、TCP，连接池、分布式神马的，很多同学可能都晕了- -，其实这还是有一定难度的，不过还好我们能想到的技术这个世界上90%的都已经有了，更何况是共享缓存呢。所以我们可以直接拿别人成熟的产品来使用就行了。至于如何开发，有兴趣也可去研究别人的源代码，自己有能力的话试着去开发一个.NET版本来提升自己的能力，一般也不推荐自己开发，除非有特殊的数据结构缓存，不然会带来维护上更高的成本，除非像很有实力和需求的公司。现在一些流行的软件或框架也都是自己的需求慢慢演化成通用软件的。现在比较知名的缓存服务比如Memcached、Redis，当然还有其他好多，这个大家可以自己搜索，我就不一一举例了。&lt;/span&gt; &lt;!--EndFragment--&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/mad/aggbug/2232213.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/mad/archive/2011/11/01/2232213.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/mad/archive/2011/10/19/ASP_NET_MVC3_Custom_FormAuthorize.html</id><title type="text">ASP.NET MVC3 Custom FormAuthorize</title><summary type="text">我们开发web系统，用户身份验证是最常见不过的。最简单的办法就是定一个基类，基类里面有判断Cookie或Session是否存在，然后决定是否跳转。今天就利用MVC的特性来一个不一样的验证方式。publicclassCustomAuthorizeAttribute:AuthorizeAttribute{protectedoverrideboolAuthorizeCore(HttpContextBasehttpContext){varuser=WebUtility.GetIdentity(httpContext);if(user!=null){httpContext.User=newUserPri</summary><published>2011-10-19T02:05:00Z</published><updated>2011-10-19T02:05:00Z</updated><author><name>君之蘭</name><uri>http://www.cnblogs.com/mad/</uri></author><link rel="alternate" href="http://www.cnblogs.com/mad/archive/2011/10/19/ASP_NET_MVC3_Custom_FormAuthorize.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/mad/archive/2011/10/19/ASP_NET_MVC3_Custom_FormAuthorize.html"/><content type="html">&lt;p&gt;我们开发web系统，用户身份验证是最常见不过的。最简单的办法就是定一个基类，基类里面有判断Cookie或Session是否存在，然后决定是否跳转。今天就利用MVC的特性来一个不一样的验证方式。&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="dp-highlighter bg_csharp" style="margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: #f5f5f5; font-size: 12px; width: 1117px; overflow-x: auto; overflow-y: auto; border-width: 1px; border-color: #999999; border-style: dashed;"&gt;&lt;ol start="1" class="dp-c" style="padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 0px; border-width: initial; border-color: initial; position: relative; list-style-type: decimal; list-style-position: initial; list-style-image: initial; color: #5c5c5c; border-style: none; margin: 0px;"&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;public&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;class&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;CustomAuthorizeAttribute&amp;nbsp;:&amp;nbsp;AuthorizeAttribute&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;protected&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;override&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;bool&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;AuthorizeCore(HttpContextBase&amp;nbsp;httpContext)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;var&amp;nbsp;user&amp;nbsp;=&amp;nbsp;WebUtility.GetIdentity(httpContext);&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;if&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;(user&amp;nbsp;!=&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;null&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;httpContext.User&amp;nbsp;=&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;new&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;UserPrincipal(WebUtility.GetIdentity(httpContext));&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;return&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;user&amp;nbsp;!=&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;null&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;} &amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;p&gt;这是自定义的验证机制，重写了系统自带的验证核心逻辑，下面是系统自带的逻辑：&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="dp-highlighter bg_csharp" style="margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: #f5f5f5; font-size: 12px; width: 1117px; overflow-x: auto; overflow-y: auto; border-width: 1px; border-color: #999999; border-style: dashed;"&gt;&lt;ol start="1" class="dp-c" style="padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 0px; border-width: initial; border-color: initial; position: relative; list-style-type: decimal; list-style-position: initial; list-style-image: initial; color: #5c5c5c; border-style: none; margin: 0px;"&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;protected&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;virtual&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;bool&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;AuthorizeCore(HttpContextBase&amp;nbsp;httpContext)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;if&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;(httpContext&amp;nbsp;==&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;null&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;throw&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;new&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;ArgumentNullException(&lt;/span&gt;&lt;span class="string" style="border-width: initial; border-color: initial; color: #009900; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;"httpContext"&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;);&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IPrincipal&amp;nbsp;user&amp;nbsp;=&amp;nbsp;httpContext.User;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;return&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;user.Identity.IsAuthenticated&amp;nbsp;&amp;amp;&amp;amp;&amp;nbsp;(&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;this&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;._usersSplit.Length&amp;nbsp;&amp;lt;=&amp;nbsp;0&amp;nbsp;||&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;this&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;._usersSplit.Contains(user.Identity.Name,&amp;nbsp;StringComparer.OrdinalIgnoreCase))&amp;nbsp;&amp;amp;&amp;amp;&amp;nbsp;(&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;this&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;._rolesSplit.Length&amp;nbsp;&amp;lt;=&amp;nbsp;0&amp;nbsp;||&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;this&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;._rolesSplit.Any(&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;new&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;Func&amp;lt;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;string&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;bool&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;gt;(user.IsInRole)));&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;p&gt;可以看出，验证都是用httpContext.User属性来判断，所以我们对登录、退出和获取用户信息都通过httpContext.User属性就行了。从MVC的示例中我们也可以发现确实如此，看看MVC3的Views里的_LogOnPartial.cshtml的代码：&lt;/p&gt;&lt;div class="dp-highlighter bg_html" style="margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: #f5f5f5; font-size: 12px; width: 1117px; overflow-x: auto; overflow-y: auto; border-width: 1px; border-color: #999999; border-style: dashed;"&gt;&lt;ol start="1" class="dp-xml" style="padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 0px; border-width: initial; border-color: initial; position: relative; list-style-type: decimal; list-style-position: initial; list-style-image: initial; color: #5c5c5c; border-style: none; margin: 0px;"&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;@if(Request.IsAuthenticated)&amp;nbsp;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="tag" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag-name" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;text&lt;/span&gt;&lt;span class="tag" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;Welcome&amp;nbsp;&lt;/span&gt;&lt;span class="tag" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag-name" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;strong&lt;/span&gt;&lt;span class="tag" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;@User.Identity.Name&lt;/span&gt;&lt;span class="tag" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag-name" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;strong&lt;/span&gt;&lt;span class="tag" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;!&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&amp;nbsp;@Html.ActionLink("Log&amp;nbsp;Off",&amp;nbsp;"LogOff",&amp;nbsp;"Account")&amp;nbsp;]&lt;span class="tag" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag-name" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;text&lt;/span&gt;&lt;span class="tag" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;else&amp;nbsp;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@:[&amp;nbsp;@Html.ActionLink("Log&amp;nbsp;On",&amp;nbsp;"LogOn",&amp;nbsp;"Account")&amp;nbsp;]&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;} &amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;p&gt;那么现在再看我们刚才重写的验证逻辑，就大致明白了。首先通过一个外部的逻辑获取User，如果没有User说明没有登录，否则就给User赋值，标记登录了。&lt;br style="padding: 0px; margin: 0px;" /&gt;再看我们是如何获取这个User的&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="dp-highlighter bg_csharp" style="margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: #f5f5f5; font-size: 12px; width: 1117px; overflow-x: auto; overflow-y: auto; border-width: 1px; border-color: #999999; border-style: dashed;"&gt;&lt;ol start="1" class="dp-c" style="padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 0px; border-width: initial; border-color: initial; position: relative; list-style-type: decimal; list-style-position: initial; list-style-image: initial; color: #5c5c5c; border-style: none; margin: 0px;"&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;public&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;static&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;User&amp;nbsp;GetIdentity(HttpContextBase&amp;nbsp;httpContext)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;var&amp;nbsp;user&amp;nbsp;=&amp;nbsp;httpContext.Request.Cookies[COOKIE_NAME_KEY];&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;var&amp;nbsp;info&amp;nbsp;=&amp;nbsp;httpContext.Request.Cookies[COOKIE_INFO_KEY];&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;if&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;(user&amp;nbsp;==&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;null&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;||&amp;nbsp;info&amp;nbsp;==&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;null&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;||&amp;nbsp;!info.Value.DESDecrypt(DES_KEY).Contains(user.Value))&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;return&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;null&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;return&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;UserManager.GetUser(user.Value);&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;p&gt;这就很易懂了，也是通过的cookie对用户资料做的存储。&lt;br style="padding: 0px; margin: 0px;" /&gt;再来看第二句里的UserPrincipal类的定义：&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="dp-highlighter bg_csharp" style="margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: #f5f5f5; font-size: 12px; width: 1117px; overflow-x: auto; overflow-y: auto; border-width: 1px; border-color: #999999; border-style: dashed;"&gt;&lt;ol start="1" class="dp-c" style="padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 0px; border-width: initial; border-color: initial; position: relative; list-style-type: decimal; list-style-position: initial; list-style-image: initial; color: #5c5c5c; border-style: none; margin: 0px;"&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;public&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;class&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;UserPrincipal&amp;nbsp;:&amp;nbsp;IPrincipal&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;private&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;readonly&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;User&amp;nbsp;_user;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;public&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;UserPrincipal(User&amp;nbsp;user)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_user&amp;nbsp;=&amp;nbsp;user;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;public&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;bool&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;IsInRole(&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;string&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;role)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;return&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;_user&amp;nbsp;!=&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;null&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;amp;&amp;amp;&amp;nbsp;_user.Role.ToString()&amp;nbsp;==&amp;nbsp;role;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;public&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;IIdentity&amp;nbsp;Identity&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;get&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;{&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;return&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;new&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;UserIdentity(_user);&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;p&gt;httpContext.User是IPrincipal类型，所以我们要自定义一个UserPrincipal类，在实现时还需要实现一个IIdentity接口。&lt;/p&gt;&lt;div class="dp-highlighter bg_csharp" style="margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: #f5f5f5; font-size: 12px; width: 1117px; overflow-x: auto; overflow-y: auto; border-width: 1px; border-color: #999999; border-style: dashed;"&gt;&lt;ol start="1" class="dp-c" style="padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 0px; border-width: initial; border-color: initial; position: relative; list-style-type: decimal; list-style-position: initial; list-style-image: initial; color: #5c5c5c; border-style: none; margin: 0px;"&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;public&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;class&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;UserIdentity&amp;nbsp;:&amp;nbsp;IIdentity&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;public&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;UserIdentity(User&amp;nbsp;user)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;User&amp;nbsp;=&amp;nbsp;user;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;public&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;User&amp;nbsp;User&amp;nbsp;{&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;get&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;private&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;set&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;public&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;string&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;Name&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;get&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;{&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;return&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;User&amp;nbsp;==&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;null&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;?&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;string&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;.Empty&amp;nbsp;:&amp;nbsp;User.UserName;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;public&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;string&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;AuthenticationType&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;get&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;{&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;return&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="string" style="border-width: initial; border-color: initial; color: #009900; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;"csdn.maddemon"&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;public&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;bool&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;IsAuthenticated&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;get&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;{&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;return&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;User&amp;nbsp;!=&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;null&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;p&gt;好了，整个验证核心需要的东西我们都准备好了，还差一个最重要的东西就是我们登录的时候需要写cookie，退出的时候需要清除cookie。&lt;/p&gt;&lt;div class="dp-highlighter bg_csharp" style="margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: #f5f5f5; font-size: 12px; width: 1117px; overflow-x: auto; overflow-y: auto; border-width: 1px; border-color: #999999; border-style: dashed;"&gt;&lt;ol start="1" class="dp-c" style="padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 0px; border-width: initial; border-color: initial; position: relative; list-style-type: decimal; list-style-position: initial; list-style-image: initial; color: #5c5c5c; border-style: none; margin: 0px;"&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;public&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;static&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;void&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;SetAuthorizeCookie(HttpContextBase&amp;nbsp;httpContext,&amp;nbsp;User&amp;nbsp;user)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;httpContext.Response.SetCookie(&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;new&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;HttpCookie(COOKIE_NAME_KEY,&amp;nbsp;user.UserName));&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;httpContext.Response.SetCookie(&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;new&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;HttpCookie(COOKIE_INFO_KEY,&amp;nbsp;(user.UserName&amp;nbsp;+&amp;nbsp;&lt;/span&gt;&lt;span class="string" style="border-width: initial; border-color: initial; color: #009900; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;"|"&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;+&amp;nbsp;user.Role).DESEncrypt(DES_KEY)));&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;public&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;static&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;void&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;RemoveAuthorizeCookie(HttpContextBase&amp;nbsp;httpContext)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;httpContext.Response.SetCookie(&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;new&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;HttpCookie(COOKIE_NAME_KEY,&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;null&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;)&amp;nbsp;{&amp;nbsp;Expires&amp;nbsp;=&amp;nbsp;DateTime.Now.AddDays(-1)&amp;nbsp;});&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;httpContext.Response.SetCookie(&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;new&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;HttpCookie(COOKIE_INFO_KEY,&amp;nbsp;&lt;/span&gt;&lt;span class="keyword" style="border-width: initial; border-color: initial; color: #0000ff; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;null&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;)&amp;nbsp;{&amp;nbsp;Expires&amp;nbsp;=&amp;nbsp;DateTime.Now.AddDays(-1)&amp;nbsp;});&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;} &amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;p&gt;这样一套下来，需要登录的Controller或Action，我们只需要加上一个[CustomAuthorize]属性就可以，如果没有登录就会自动跳转到config里配置的登录页。是不是很不方便啊？&amp;nbsp;方便吗？不方便吗？&amp;nbsp;方便吗？不方便吗？&amp;nbsp;方便吗？不方便吗？是不是啊 哈哈&lt;/p&gt;&lt;div class="dp-highlighter bg_html" style="margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: #f5f5f5; font-size: 12px; width: 1117px; overflow-x: auto; overflow-y: auto; border-width: 1px; border-color: #999999; border-style: dashed;"&gt;&lt;ol start="1" class="dp-xml" style="padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 0px; border-width: initial; border-color: initial; position: relative; list-style-type: decimal; list-style-position: initial; list-style-image: initial; color: #5c5c5c; border-style: none; margin: 0px;"&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&lt;span class="tag" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag-name" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;authentication&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="attribute" style="border-width: initial; border-color: initial; color: red; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;mode&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;=&lt;/span&gt;&lt;span class="attribute-value" style="border-width: initial; border-color: initial; color: blue; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;"Forms"&lt;/span&gt;&lt;span class="tag" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; color: #5c5c5c; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&lt;span class="tag" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag-name" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;forms&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="attribute" style="border-width: initial; border-color: initial; color: red; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;loginUrl&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;=&lt;/span&gt;&lt;span class="attribute-value" style="border-width: initial; border-color: initial; color: blue; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;"~/Account/LogOn"&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="attribute" style="border-width: initial; border-color: initial; color: red; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;timeout&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;=&lt;/span&gt;&lt;span class="attribute-value" style="border-width: initial; border-color: initial; color: blue; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;"2880"&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span class="tag" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; line-height: 13px; border-style: none;"&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&lt;span class="tag" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag-name" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;authentication&lt;/span&gt;&lt;span class="tag" style="border-width: initial; border-color: initial; color: #993300; background-color: inherit; font-weight: bold; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="border-width: initial; border-color: initial; color: black; background-color: inherit; border-style: none; padding: 0px; margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;img src="http://www.cnblogs.com/mad/aggbug/2217246.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/mad/archive/2011/10/19/ASP_NET_MVC3_Custom_FormAuthorize.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry></feed>
