最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
asp.netcoreweb頁面驗(yàn)證-創(chuàng)新互聯(lián)
本例是用簡單角色驗(yàn)證方式來通過用戶登錄后,獲取用戶角色,每種角色可以通過[Authorize(Roles = "admin,user")]在Action上來控制訪問的權(quán)限,也就是說,只有屬性這個(gè)角色才能訪問這個(gè)Action。

道先添加Microsoft.AspNetCore.Authentication.Cookies引用

創(chuàng)新互聯(lián)公司是專業(yè)的上高網(wǎng)站建設(shè)公司,上高接單;提供成都做網(wǎng)站、成都網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行上高網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
在StartUp.cs的Configure方法中添加
//為驗(yàn)證添加中間件
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    //驗(yàn)證方案名稱
    AuthenticationScheme = "loginvalidate",
    //沒有權(quán)限時(shí)導(dǎo)航的登錄action
    LoginPath = new Microsoft.AspNetCore.Http.PathString("/login"),
    //訪問被拒絕后的acion
    AccessDeniedPath = new Microsoft.AspNetCore.Http.PathString("/Home/NoPermission"),      
    AutomaticAuthenticate = true,
    AutomaticChallenge = true,
    SlidingExpiration = true
});

HomeController中的登錄的action實(shí)現(xiàn)

using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
 
namespace webAuth.Controllers
{
    /// 
    /// 本Controller允許admin和user兩種角色可以訪問
    /// 
    [Authorize(Roles = "admin,user")]
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        /// 
        /// aobout只允許user角色訪問
        /// 
        /// 
        [Authorize(Roles = "user")]
        public IActionResult About()
        {
            var id = User.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            ViewData["Message"] = "UserID:"+ id;
 
            return View();
        }
        /// 
        /// contact只允許admin角色訪問
        /// 
        /// 
        [Authorize(Roles = "admin")]
        public IActionResult Contact()
        {
            var id=User.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            ViewData["Message"] = "UserID:"+ id;
 
            return View();
        }
 
        public IActionResult NoPermission()
        {
            return View();
        }
 
        /// 
        /// 允許所有登錄者
        /// 
        /// 如果用戶訪問的不是登錄頁,returnUrl將把這個(gè)url傳進(jìn)來,待登錄成功后返回這個(gè)地址
        /// 
        [AllowAnonymous]
        [HttpGet("login")]
        public IActionResult Login(string returnUrl)
        {
            //判斷是否驗(yàn)證
            if (!HttpContext.User.Identity.IsAuthenticated)
            {
                //把返回地址保存在前臺的hide表單中
                ViewBag.returnUrl = returnUrl;
            }
            ViewBag.error = null;
            return View();
        }
        /// 
        /// 允許所有登錄者
        /// 
        /// 用戶名
        /// 密碼
        /// 返回u
        /// 
        [AllowAnonymous]
        [HttpPost("login")]
        public IActionResult Login(string username, string password, string returnUrl)
        {
            //從數(shù)據(jù)庫驗(yàn)證用戶,關(guān)取出用戶所需要信息
            var users = new List() {
                new { ID = 1, UserName = "zsf",Password="111", Name = "張三豐", RoleTypeID = 1, RoleType = "admin", RoleTypeName = "管理員" },
                 new { ID = 2, UserName = "zwj",Password="222", Name = "張無忌", RoleTypeID = 2, RoleType = "user", RoleTypeName = "普通用戶" }
            };
            var user = users.SingleOrDefault(u => u.UserName == username && u.Password == password);
            if (user!=null)
            {
                //登錄成功后,設(shè)置聲明
                var claims = new Claim[] {
                      new Claim(ClaimTypes.UserData,username),
                      new Claim(ClaimTypes.Role,user.RoleType),
                      new Claim(ClaimTypes.Name,user.Name),
                      new Claim(ClaimTypes.Sid,user.ID.ToString())
                };
                HttpContext.Authentication.SignInAsync("loginvalidate", new ClaimsPrincipal(new ClaimsIdentity(claims, "Cookie")));
                HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(claims));
                return new RedirectResult(returnUrl == null ? "/" : returnUrl);
            }
            else
            {
                ViewBag.error = "用戶名或密碼錯誤!";
                return View();
            }
        }
    }
}

Login.cshtml頁面如下:

@{
    Layout = null;
}



    
    
    登錄
    
    


    
        
            
                
                    
                        用戶名
                        
                    
                
            
                                                                           密碼                                              
                
            
                                                                                                    登錄                     
                
            
            @if (ViewBag.error != null)             {                 @ViewBag.error             }         
              

如果在其他頁面使用User,可以像下面這樣使用

當(dāng)前用戶:@User.Identity.Name

當(dāng)然也可以從User中查到其他登錄時(shí)存儲的Claim的值

登錄成功后

asp.net core web頁面驗(yàn)證                  

登錄成功后訪問沒有權(quán)限頁面(當(dāng)然可以不讓這種角色看到不能訪問的鏈接)

asp.net core web頁面驗(yàn)證

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。


當(dāng)前題目:asp.netcoreweb頁面驗(yàn)證-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://fisionsoft.com.cn/article/hhepp.html

其他資訊