상호 작용을 위해 양식 인증 및 웹 API를 사용하는 MVC4 웹 응용 프로그램을 설정했습니다. 모든 API 컨트롤러는 [Authorize]
특성을 사용합니다.양식 인증 및 역할이있는 웹 API
우리가 역할 지원을 추가하기 전까지는 별 효과가 없었습니다. 대신 본격적인 RoleProvider을 구현, 나는 티켓의 UserData
에게 역할의 목록을 추가, 다음과 같은 모듈 생성 :
public class SecurityModule : IHttpModule
{
public void Init(HttpApplication context)
{
var roleManager = (RoleManagerModule)context.Modules["RoleManager"];
roleManager.GetRoles += GetRoles;
}
void GetRoles(object sender, RoleManagerEventArgs e)
{
var user = e.Context.User;
if (user.Identity.IsAuthenticated && !(user is MyCustomPrincipal))
{
var roles = GetRolesFromFormsAuthCookie();
if (roles != null)
e.Context.User = new MyCustomPrincipal(user.Identity, roles,
otherData);
}
e.RolesPopulated = true;
}
}
이 MVC 통화에 완벽하게 작동합니다. 그러나 API의 경우 GetRoles
이 호출되면이되고 해당 메소드에 도달하면 GenericPrincipal
으로 돌아갑니다.
웹 API에서도이 작업을 수행하려면 어떻게해야합니까? DelegatingHandler
을 만들어야합니까? 난 그냥 요청 맥락에서 그것을 저장할 수 있지만
또한, (나는 RolePrincipal로 끝날 것 때문에) 그냥 RoleProvider에 의존하지 않는 이유가 될 수 내 교장의 일부 사용자 데이터를 저장하고있어 .
업데이트 : 나는 지금 IHttpModule
과 동일한을 수행하고 Thread.CurrentPrincipal
을 설정하는 DelegatingHandler
을 추가했습니다. 이것은 합리적인 접근법입니까?
Qué boludo que soy :-) –