저는 .NET에 비교적 익숙하며 FormsAuthentication을 파악하려고합니다. 몇 가지 다른 자습서를 통해 동일한 문제가 발생할 때마다 살펴 보았습니다. 어떤 이유로 UserData가 티켓에 저장되지 않습니다. Global.aspx.cs의 티켓에 휴식을 설정할 때 UserName은 있지만 UserData는 빈 문자열이며 버전이 1로 지정되면 버전이 2로 설정됩니다. 또 다른 이상한 점은 모든 인증 된 사용자가 허용된다는 것입니다. web.config에서 관리자 만 지정할 수있을 때 Admin_Content 폴더의 페이지에 액세스 할 수 있습니다.FormsAuthenticationTicket이 UserData를 저장하지 않습니다.
Login.aspx
UserFull user = ManageUsers.login(loginTemplate.UserName, loginTemplate.Password);
if (user != null)
{
string[] roles = { user.role };
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,
userName,
DateTime.Now,
DateTime.Now.AddDays(30),
true,
roles[0],
FormsAuthentication.FormsCookiePath
);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
FormsAuthentication.SetAuthCookie(encryptedTicket, true);
Response.Redirect("Admin_Content/Admin.aspx");
}
Global.aspx.cs하여 Application_AuthenticateRequest
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = (id.Ticket);
if (!string.IsNullOrEmpty(ticket.UserData))
{
string userData = ticket.UserData;
string[] roles = userData.Split(',');
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
}
}
}
}
홈페이지의 Web.config
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<roleManager enabled="true"></roleManager>
<authentication mode="Forms">
<forms name="AOTMP_Demo" loginUrl="Login.aspx"
protection="All" path="/" cookieless="UseCookies"/>
</authentication>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
<configuration>
Admin_Content 폴더의 web.config
'FormsAuthentication.SetAuthCookie'는 정확히 'FormsAuthentication.SetAuthCookie'가 가정하고이 상황에 적합합니다. http://msdn.microsoft.com/en-us/library/twk5762b(v=vs.100).aspx. 당신이 조립 해 놓은 작은 해킹 나는 테스트를 거쳤습니다. 그리고 그것은 나를 위해 전혀 작동하지 않으며 해킹은 거의 신뢰할 수 없습니다. – cjbarth
@cjbarth, 어떻게 FormsDataSetAuthCookie로 UserData를 설정합니까? –
@cjbarth : *** *** *** 해킹이 아닙니다. 그것은 바로 사용 가능한 옵션으로는 충분하지 않을 때 쿠키를 설정하는 방법입니다. – Mrchief