역할 정보가 포함된 FormsAuthentication 인증
6357 단어 Authentication
1. 로그인할 때 FormsAuthenticationTicket를 수동으로 설정합니다. 코드는 다음과 같습니다.
//
FormsAuthenticationTicket
FormsAuthenticationTicket Ticket
=
new
FormsAuthenticationTicket(
1
,
"username
"
, DateTime.Now, DateTime.Now.AddMinutes(
20
),
false
,
"
admin
"
);
//
string
HashTicket
=
FormsAuthentication.Encrypt(Ticket);
//
cookie
HttpCookie UserCookie
=
new
HttpCookie(FormsAuthentication.FormsCookieName, HashTicket);
//
Cookie
Response.Cookies.Add(UserCookie);
2. Global에 다음 코드를 추가합니다.
protected
void
Application_AuthenticateRequest(Object sender, EventArgs e)
{
//
Construst the GeneralPrincipal and FormsIdentity objects
HttpCookie authCookie
=
Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if
(
null
==
authCookie)
{
//
no authentication cokie present
return
;
}
FormsAuthenticationTicket authTicket
=
FormsAuthentication.Decrypt(authCookie.Value);
if
(
null
==
authTicket)
{
//
could not decrypt cookie
return
;
}
//
get the role
string
[] role
=
authTicket.UserData.Split(
new
char
[] {
'
,
'
});
FormsIdentity id
=
new
FormsIdentity(authTicket);
Context.User
=
new
GenericPrincipal(id, role);
}
이렇게 하면 우리는 프로그램에서 [Authorize(Roles="admin")]를 사용하여 검증할 수 있다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
React(Next.js) × Firebase에서 전체 개발 인증 주위로그인 로그인하지 않았을 때 특정 페이지에 들어가려면 경보 & 이동 로그인 시 로그인 페이지 등에 가면 경보 & 이전 실제 로그인 처리는 사용자 로그인과 같은 줄만 있습니다. await auth.signInWithE...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.