一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

asp.net Forms身份驗證和基于角色的權限訪問

主要思想:Forms身份驗證用來判斷是否合法用戶,當用戶合法后,再通過用戶的角色決定能訪問的頁面。
具體步驟:
    1、創建一個網站,結構如下:
        網站根目錄
            Admin目錄            ---->    管理員目錄
                Manager.ASPx        ---->    管理員可以訪問的頁面
            Users目錄            ---->    注冊用戶目錄
                Welcome.ASPx        ---->    注冊用戶可以訪問的頁面
            Error目錄            ---->    錯誤提示目錄
                AccessError.htm        ---->    訪問錯誤的提示頁面
            default.ASPx            ---->    網站默認頁面
            login.ASPx            ---->    網站登錄頁面
            web.config            ---->    網站配置文件
    2、配置web.config如下:
復制代碼 代碼如下:
        <configuration>
            <system.web>
                <!--設置Forms身份驗證-->
                <authentication mode="Forms">
                    <forms loginUrl="Login.ASPx" name="MyWebApp.APSXAUTH" path="/" protection="All" timeout="30"/>
                </authentication>
                <authorization>
                    <allow users="*"/>
                </authorization>
            </system.web>
        </configuration>

        <!--設置Admin目錄的訪問權限-->
        <location path="Admin">
            <system.web>
                <authorization>
                    <allow roles="Admin"/>
                    <deny users="?"/>
                </authorization>
            </system.web>
        </location>
        <!--設置Users目錄的訪問權限-->
        <location path="Users">
            <system.web>
                <authorization>
                    <allow roles="User"/>
                    <deny users="?"/>
                </authorization>
            </system.web>
        </location>

    3、在login.ASPx頁面的登錄部分代碼如下:
復制代碼 代碼如下:
        protected void btnLogin_Click(object sender, EventArgs e)
        {    
            //Forms身份驗證初始化
            FormsAuthentication.Initialize();
            //驗證用戶輸入并得到登錄用戶,txtName是用戶名稱,txtPassword是登錄密碼
            UserModel um = ValidUser(txtName.Text.Trim(),txtPassword.Text.Trim());
            if (um != null)
            {
             //創建身份驗證票據
             FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                                            um.Name,
                                            DateTime.Now,
                                            DateTime.Now.AddMinutes(30),
                                            true,
                                            um.Roles,//用戶所屬的角色字符串
                                            FormsAuthentication.FormsCookiePath);
             //加密身份驗證票據
             string hash = FormsAuthentication.Encrypt(ticket);
             //創建要發送到客戶端的cookie
             HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
             if (ticket.IsPersistent)
             {
                cookie.Expires = ticket.Expiration;
             }
             //把準備好的cookie加入到響應流中
             Response.Cookies.Add(cookie);

             //轉發到請求的頁面
             Response.Redirect(FormsAuthentication.GetRedirectUrl(um.Name,false));
            }
            else
            {
             ClientScriptManager csm = this.Page.ClientScript;
             csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('用戶名或密碼錯誤!身份驗證失敗!');", true);
            }
        }    
        //驗證用戶
        private UserModel ValidUser(string name, string password)
        {
            return new UserService().Validate(name, password);
        }

    4、給網站添加處理程序Global.asax,其中通用身份驗證代碼如下:
復制代碼 代碼如下:
        //改造原來的User,給其添加一個用戶所屬的角色數據
        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current.User != null )
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {
                        FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                        FormsAuthenticationTicket ticket = id.Ticket;

                        string userData = ticket.UserData;
                        string[] roles = userData.Split(',');
                        //重建HttpContext.Current.User,加入用戶擁有的角色數組
                        HttpContext.Current.User = new GenericPrincipal(id, roles);
                    }
                }
            }
        }

    5、在Admin目錄中Manager.ASPx頁面加載代碼如下:
復制代碼 代碼如下:
        protected void Page_Load(object sender, EventArgs e)
        {
            //判斷通過身份驗證的用戶是否有權限訪問本頁面
            FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
            //判斷通過身份驗證的用戶是否是Admin角色
            if (!id.Ticket.UserData.Contains("Admin"))
            {
                //跳轉到訪問權限不夠的錯誤提示頁面
                Response.Redirect("~/Error/AccessError.htm", true);
            }
        }
        //安全退出按鈕的代碼
        protected void btnExit_Click(object sender, EventArgs e)
        {
            //注銷票據
            FormsAuthentication.SignOut();
            ClientScriptManager csm = this.Page.ClientScript;
            csm.RegisterStartupScript(this.GetType(), "exit_tip", "alert('您已經安全退出了!');", true);
        }

    6、在Users目錄中Welcome.ASPx頁面加載代碼如下:
復制代碼 代碼如下:
        protected void Page_Load(object sender, EventArgs e)
        {
            //判斷通過身份驗證的用戶是否有權限訪問本頁面
            FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
            //判斷通過身份驗證的用戶是否是User角色
            if (!id.Ticket.UserData.Contains("User"))
            {
                //跳轉到訪問權限不夠的錯誤提示頁面
                Response.Redirect("~/Error/AccessError.htm", true);
            }
        }
        //安全退出按鈕的代碼
        protected void btnExit_Click(object sender, EventArgs e)
        {
            //注銷票據
            FormsAuthentication.SignOut();
            ClientScriptManager csm = this.Page.ClientScript;
            csm.RegisterStartupScript(this.GetType(), "exit_tip", "alert('您已經安全退出了!');", true);
        }

測試結果:
    數據:
        假設有3個用戶,如下:
        ------------------------------------------
        用戶名        密碼        角色字符串
        ------------------------------------------
        sa        sa        Admin,User
        admin        admin        Admin
        user        user        User
        ------------------------------------------
    測試:
        如果使用admin登錄,只能訪問Admin目錄的Manager.ASPx頁面;
        如果使用user登錄,只能訪問Users目錄的Welcome.ASPx頁面;
        使用sa登錄,既能訪問Admin目錄的Manager.ASPx頁面,又能訪問Users目錄的Welcome.ASPx頁面。
    注意:測試時注意及時點擊安全退出按鈕,否則影響測試結果。

AspNet技術asp.net Forms身份驗證和基于角色的權限訪問,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 精品国语对白精品自拍视 | 国产永久免费高清在线观看视频 | 日韩午夜免费 | 国产高清精品久久久久久久 | 五月婷六月婷婷 | 免费观看国产精品 | 一菊综合网成人综合网 | 午夜黄色小视频 | 欧美日韩亚洲人人夜夜澡 | 成人精品一区二区www | 国内自拍第五一页 | 精品美女视频在线观看2023 | 色婷婷精品免费视频 | 亚洲国产精品一区二区三区 | 日本免费www | 天天躁日日躁狠狠躁中文字幕老牛 | 久久精品久久精品 | 国产精品麻豆视频 | 福利在线视频观看 | 怎么看毛片 | 能在线观看的一区二区三区 | 国产在线精品一区二区三区 | 久久久精品国产 | sifangtv| 婷婷丁香六月天 | 国产福利在线观看永久免费 | 精品资源在线 | 亚洲综合色婷婷中文字幕 | 91午夜视频 | 久久久久国产 | 色视频在线观看免费 | 青草国产| 国产精品专区第二 | 深夜小视频在线观看 | 黄免费看| 日本二三区 | 国产精品二区页在线播放 | 亚洲欧美成人永久第一网站 | 国产综合色在线视频区 | 16女性下面扒开无遮挡免费 | 97超级碰在线精品视频 |