2011-10-22 2 views
0

사용자 지정 메서드를 사용하여 FormsAuthentication을 사용하여 사용자를 등록하고 로그인하는이 응용 프로그램이 있습니다. 이 서버가 호스팅되는 서버에는 매 15 분마다 세션을 다시 시작하는 정책이 있으며 모든 사용자가 로그 아웃되는 경우 발생합니다. 사용자를 loggin에하는 코드는 다음과 같습니다 양식으로세션이 다시 시작될 때 내 사용자가 로그 아웃되는 이유는 무엇입니까?

var user = this.accountRepo.GetUser(id); 

// Create the forms authentication cookie 
var cookieValue = user.name; 
HttpCookie cookie = FormsAuthentication.GetAuthCookie(cookieValue, true); 

// Dercrypt the cookie 
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value); 

// Create a new ticket with the desired data 
FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket 
             (
              ticket.Version, 
              ticket.Name, 
              ticket.IssueDate, 
              DateTime.Now.AddYears(1), 
              true, 
              user.Authentication 
             ); 
// Update the cookies value 
cookie.Value = FormsAuthentication.Encrypt(newTicket); 
Response.Cookies.Set(cookie); 

accountRepo.Login(user); 

만들고 기본적으로, 나는 다음 로그인 버튼 또는 사용자 이름을 표시하려면 다음 논리를 암호를 사용하는 해시 사용자 내 인증 데이터와 쿠키 :

@{ 
    var accountRepo = new AccountRepository(); 
    var user = accountRepo.GetCurrentUser(); 
} 

@if(user != null && user.LoggedIn) { 
    <div>@Html.ActionLink(Context.User.Identity.Name + " - Logout", "LogOff", "Account", null, new { @class = "logout_link" })</div> 
} 
else 
{ 
    @Html.ActionLink("Login", "Login", "Account", new { returnUrl = Request.Url.AbsoluteUri }, new { @class = "login_link" }) 
} 

그리고 는 "GetCurrentUser는()"방법은 다음과 같습니다

var cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; 

if (cookie != null) 
{ 
    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value); 

    return db.Users.SingleOrDefault(u => u.Authentications.Equals(ticket.UserData, StringComparison.CurrentCultureIgnoreCase)); 
} 

return null; 

내가 여기서 뭔가를 놓치고 있습니까? 이 코드를 사용하면 세션이 다시 시작되면 사용자가 로그인 상태를 유지해야합니다.

미리 감사드립니다.

+6

와우, 너무 많은 틀린 여기 나는 어디에서 시작해야할지 몰라. 보기에서 저장소 인스턴스화 ??? Eeeeeevil. 문제는 세션이 손실되고 새로운 세션이 생성 될 때 쿠키가 변경된다는 것입니다. 쿠키에 사용자 비밀번호를 저장하는 것은 심지어 암호화되고 해쉬되기도하지만 나쁘지 않은 나쁜 생각입니다. –

+0

그것에 대해 말해줘. 나와 내게 대항하는 괴물이다. – FelixMM

+0

끔찍한 것이 아니라, 이것은 아주 작은 비트로, 리펙토링 할 수 있어야한다. 리팩터링과 관련하여 도움이 필요하면 추가 질문을 게시하십시오. 이것은 이것을 유지하거나 단순히 읽을 필요가있는 사람들에게는 불공평합니다. –

답변

0

미스터 맨이 말한 것과 같습니다. 세션이 재부팅 될 때마다 쿠키 이름이 다시 생성되므로 응용 프로그램은 이전과 다른 이름으로 쿠키를 찾고있었습니다. 저를 도와, 그리고 미래에이 응용 프로그램을 지원하는 개발자, 내가 리팩토링이 여러분의 마음의 평화를 위해

이 너무 없다 "악"더 이상 그 : P

관련 문제