我有一个mvc应用程序,我们通过外部程序包跟踪自定义身份验证。验证后,用户名&其他详细信息通过一个对象发送,该对象可以通过' Thread.CurrentPrincipal'控制器中的属性。我使用此对象的用户名登录我的数据库进行日志记录。
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()在这里为空。我想知道是否有一种方法可以将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 :(得分:0)
我怀疑WebApiController
没有[Authorize]
属性或类似的东西。
[Authorize]
public class WebApiController : ApiController