내 WebAPI 2 응용 프로그램에는 액세스 토큰을 확인하는 사용자 지정 인증 필터가 있습니다. 토큰이 있고 API에 속성이있는 경우 해당 토큰에 매핑되는 사용자가 있는지 확인합니다.사용자 개체의 주체/사용자 컨텍스트 설정
API의 특성상 대부분의 메소드는 특정 사용자의 컨텍스트 (즉, 사용자 프로필을 업데이트하는 "POST API/프로필")에서 실행됩니다. 이를 위해서는 액세스 토큰에서 얻은 대상 사용자에 대한 정보가 필요합니다.
if(myDBContext.MyUsers.Count(x => x.TheAccessToken == clientProvidedToken)){
IPrincipal principal = new GenericPrincipal(new GenericIdentity(myAccessToken), new string[] { "myRole" });
Thread.CurrentPrincipal = principal;
HttpContext.Current.User = principal;
return true;
}
이 잘 작동 [현재 구현, 유형 AuthorizeAttribute의 속성 내에서 발생], 나는 그 방법에 두 번째 조회를 수행 할 액세스 토큰을 사용할 수 있어요. 하지만 이미 인증 시간에 조회를 했으므로 다른 DB 호출을 낭비하고 싶지 않습니다.
는[내가하고 싶은 (그러나 분명히 작동하지 않습니다) 어떤]
MyUser user = myDBContext.MyUsers.FirstOrDefault(x => x.TheAccessToken == clientProvidedToken);
if(user != null){
// Set *SOME* property to the User object, such that it can be
// access in the body of my controller method
// (e.g. /api/profile uses this object to load data)
HttpContext.Current.User = user;
return true;
}
GenericPrincipal 확장을 생각하지 않은 이유를 모르겠다. 멋지게 일했습니다! 고맙습니다! – ShaneC