잊지 않겠습니다.

ASP.NET에서의 Logon/Logout을 구현하는 가장 일반적인 Forms authentication 방법.

   

web에서의 auth를 저장하는 방법으로 가장 주로 사용되는 것은 cookie를 사용, cookie와 auth에 대한 암호화를 이용해서 사용자 정보와 role을 저장할 수 있다.

   

Log-on (사용자 정보 저장)

1. web.config 설정

 authentication mode를 form으로 설정 

     <authentication mode="Forms">
      <forms loginUrl="~/NotLogOn" />
    </authentication>

   

2. Auth Cookie와 Authenticated user에 대한 정보 설정

LDAP, SQL Server를 이용한 Forms Authentication 이용 가능 (이에 대한 posting은 나중에...)

1) userName, password를 이용해서 정상적인 유저인 경우에 FormsAuthenticationTicket 생성

 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, 
userName,
DateTime.Now,
DateTime.Now.AddMinutes(20),
false,
roles )

  

* Role 정보 입력

 string role = string.concat("Senior Manager|Manager|Employee")

  

2) Cookie 암호화 & 저장

string encryptedTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) 

Response.Cookie.Add(authCookie)

  

3. Auth 정보 확인 및 정보 검색

1) 암호화된 Cookie 값 얻기

string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
if(authCookie == null)
{
// there is no authentication cookie  
} 

  

2) Cookie 값 Decrypt

FormsAuthenticationTicket authTicket = null;

try 

{

authTicket = FormsAuthentication.Decrypt(authCookie.Value); 

  

3) UserData 얻기

string userName = authTicket.Name; //userName
string userData = authTicket.UserData; //userData : roles
DateTime issueDate = authTicket.IssueDate; //cookie created date
DateTime expiredDate = authTicket.Expiration; //cookie expired date
bool expired = authTicket.Expired;
Posted by Y2K
,