asp.net許可權認證篇外:整合域帳號登入

來源:互聯網
上載者:User

標籤:html   ons   .net   cookie   cep   png   lan   public   under   

 

在之前的我們已經講過asp.net許可權認證:Windows認證,現在我們來講講域帳號登入,

這不是同一件事哦,windows認證更多的是對資源訪問的一種許可權管控,而域帳號登入更多的是針對使用者登入的認證,他一般與forms認證結合一起使用

 

demo就用之前的asp.net許可權認證:Forms認證 

1、改一下LoginController,如下

 1     public class LoginController : Controller 2     { 3         public ActionResult Index() 4         { 5             var returnUrl = Request["ReturnUrl"]; 6  7             if (Request.HttpMethod == "POST") 8             { 9                 try10                 {11                     // 格式為: 域\登入名稱12                     var domainUser = Request["userid"];13                     var password = Request["password"];14                     var userArr = domainUser.Split(‘\\‘);15 16                     var entry = new DirectoryEntry(string.Concat("LDAP://", userArr[0]), userArr[1], password);17                     var search = new DirectorySearcher(entry);18                     var result = search.FindOne();19                     if (result != null)20                     {21                         var ticket = new FormsAuthenticationTicket(22                             1,23                             userArr[1],24                             DateTime.Now,25                             DateTime.Now.AddMinutes(20),26                             true,27                             "role1,role2,role3,role4",28                             "/"29                         );30 31                         var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));32                         cookie.HttpOnly = true;33                         HttpContext.Response.Cookies.Add(cookie);34 35                         return Redirect(returnUrl);36                     }37                 }38                 catch (Exception ex)39                 {40                     // 拋出異常。41                 }42             }43 44             ViewBag.ReturnUrl = returnUrl;45 46             return View();47         }48     }

 

 

2、運行項目,出現登入頁面,正常的,因為我們預設首頁就加了許可權限制,輸入欄位賬戶口令

3、登入

 

完全沒有問題,是不是很簡單呀?

 

asp.net許可權認證篇外:整合域帳號登入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.