2013-10-08 1 views
0

Windows 인증을 사용 중이며이 odata 컨트롤러에서 정상적으로 작동하고 있습니다. 그러나 최신 NuGet 패키지 (시험판 5.0.0-rc1)를 얻은 후 뭔가 바뀌었고 ApiController.User이 null입니다. 더 이상 Windows 인증을 통과하지 않습니다. 어떤 아이디어? [Authorize] 속성을 추가하려고 시도했지만 작동하지 않습니다. 어딘가에 다른 설정이 필요합니다.odata ApiController.User == 웹 API 5.0.0-rc1로 업그레이드 한 후 NULL =

public class ProductsController : EntitySetController<Product, int> 
{ 
protected ProjectContextUnitOfWork UoW; 
protected UserRepository UserRepo; 
protected ProductRepository ProductRepo; 
protected Project.Models.User CurrentUser; 

// odata/Products/ 

public ProductsController() 
{ 
    if (!User.Identity.IsAuthenticated) 
    { 
     HttpResponseMessage msg = Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "User not authenticated."); 
     throw new HttpResponseException(msg); 
    } 

    ProjectUserPrincipal LoggedInUser = this.User as ProjectUserPrincipal; 


    // - closed in Dispose() 
    UoW = new ProjectContextUnitOfWork(false); //without lazy loading 

    UserRepo = new UserRepository(UoW); 
    ProductRepo = new ProductRepository(UoW); 

    CurrentUser = UserRepo.Get(LoggedInUser.Username, LoggedInUser.Domain); 
} 

protected override Product GetEntityByKey(int id) 
{ 
    var x = from b in ProductRepo.GetAvailableProductsWithNumbers(CurrentUser) 
      where b.Id == id 
      select b; 

    return x.FirstOrDefault(); 
} 

... 
} 

기타 세부 사항 : 난 다시 5.0.0.beta2에 복귀 할 때

  • 는 .NET 4.5
  • 웹 작동, 다른 변경하지 않고, 또한

양식 다시. 따라서 Microsoft.AspNet.WebApi의 변경 사항입니다. 코드를 변경해도 괜찮습니다. 팁이 필요합니다. 감사!

답변

1

컨트롤러 생성자에서 ApiController.User를 사용하고 있기 때문입니다. 이 시점에서 속성은 초기화되지 않았습니다. 당신은해야합니다

그래서 코드가 보이는

  • 가 초기화 방법에 초기화 코드를 이동 컨트롤러에서 [권한 부여] 속성을 추가합니다 같은 :

    [Authorize] 
    public class ProductsController : EntitySetController<Product, int> 
    { 
        protected override void Initialize(System.Web.Http.Controllers.HttpControllerContext controllerContext) 
        { 
         base.Initialize(controllerContext); 
    
         ProjectUserPrincipal LoggedInUser = this.User as ProjectUserPrincipal; 
    
    
         // - closed in Dispose() 
         UoW = new ProjectContextUnitOfWork(false); //without lazy loading 
    
         UserRepo = new UserRepository(UoW); 
         ProductRepo = new ProductRepository(UoW); 
    
         CurrentUser = UserRepo.Get(LoggedInUser.Username, LoggedInUser.Domain); 
        } 
    }