熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET內置票據認證

2022-06-13   來源: .NET編程 

   在根目錄建立一個Globalasax文件烤入一段代碼

  代碼

  protected void Application_AuthenticateRequest(object SENDER EventArgs e)

  {

  if (HttpContextCurrentUser != null)

  {

  if (HttpContextCurrentUserIdentityIsAuthenticated)

  {

  if (HttpContextCurrentUserIdentity is FormsIdentity)

  {

  FormsIdentity id = (FormsIdentity)HttpContextCurrentUserIdentity;

  FormsAuthenticationTicket tiecket = idTicket;

  string userData = tiecketUserData;

  string[] roles = userDataSplit();

  HttpContextCurrentUser = new SystemSecurityPrincipalGenericPrincipal(id roles);

  }

  }

  }

  }

  :  在nfig 文件中配置目錄權限及登錄頁

  登錄頁在systemweb節點中

  <authentication mode=Forms>

  <forms name=mycook loginUrl=loginaspx protection=All path=//>

  </authentication>

  配置目錄權限在systemweb節點外面

  代碼

  <location path=admin>

  <systemweb>

  <authorization>

  <allow roles=admin/>

  <deny users=*/>

  </authorization>

  </systemweb>

  </location>

  <location path=user>

  <systemweb>

  <authorization>

  <allow roles=user/>

  <deny users=*/>

  </authorization>

  </systemweb>

  </location>

  <location path=admin/admin_loginaspx>

  <systemweb>

  <authorization>

  <allow users=*/>

  </authorization>

  </systemweb>

  </location>

  <location path=user/user_loginaspx>

  <systemweb>

  <authorization>

  <allow users=*/>

  </authorization>

  </systemweb>

  </location>

  代碼

  HttpCookie cook;

  string strReturnURL;

  FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(

   name DateTimeNow DateTimeNowAddMinutes() false lv);

  cook = new HttpCookie(mycook);

  cookValue = FormsAuthenticationEncrypt(ticket);

  ResponseCookiesAdd(cook);

  strReturnURL = RequestParams[ReturnUrl];

  if (strReturnURL != null)

  {

  ResponseRedirect(strReturnURL);

  }

  else

  {

  ResponseRedirect(Defaultaspx);

  }


From:http://tw.wingwit.com/Article/program/net/201311/12711.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.