2017-12-19 12 views
1

私はmvcアプリケーションを持っています。認証後、ユーザ名&の他の詳細は、コントローラの 'Thread.CurrentPrincipal'プロパティでアクセスできるオブジェクトを通じて送信されます。このオブジェクトのユーザー名を使用して、ログに記録するためにデータベースにログインします。userloginidをコントローラーからapicontrollerに渡します

public class HomeController : Controller 
{ 
    public string SyncProduct(string pIds) 
    { 
     var AuthenticatedUser = Thread.CurrentPrincipal as AuthenticatedUser; 

     var model = new Model.Authentication.Authentication 
     { 
      User = AuthenticatedUser 
     }; 

     string returnValue = null; 
     try 
     { 
      returnValue = new SyncProduct().ProcessProducts(pIds); 
      new Common().LogInfo(string.Format("ProcessIDS"), User.Identity.Name.ToString()); 
     } 
     catch (Exception ex) 
     { 
      new Common().LogException(string.Format("HomeController/SyncProduct", ex.ToString()), User.Identity.Name.ToString()); 
     } 
     return string.Format("text:{0}", returnValue); 
    } 
} 

私は、APIのコントローラで同じユーザー名をログに記録したいのですが、User.Identity.Name.ToString()はここではnullです。 User.Identity.Name.ToString()の値をhomecontrollerからapicontrollerに渡すか、グローバルにする方法があるかどうかを知りたかったのです。

public class WebApiController : ApiController 
{ 

    [HttpPut] 
    public HttpResponseMessage Put(FormDataCollection form) 
    { 
     try 
     { 

     var AuthenticatedUser = Thread.CurrentPrincipal as AuthenticatedUser; 

     var model = new Model.Authentication.Authentication 
     { 
      User = AuthenticatedUser 
     }; 

     //code to update object 
     new Common().LogInfo(string.Format("logging info", User.Identity.Name.ToString()); 
     } 
     catch (Exception ex) 
     { 
      new Common().LogException(string.Format("logging info", ex.ToString()), User.Identity.Name.ToString()); 
     } 
     return Request.CreateResponse(HttpStatusCode.Created); 
    } 
} 

答えて

0

私はそれ由来のようなWebApiController[Authorize]属性か何かを持っていないことを疑いました。

[Authorize] 
public class WebApiController : ApiController 
+0

Rainmanに感謝しますが、[Authorize]属性を使用していません。 – CodeNinja