下面的代码是我的$ http Interceptor Factory。在此代码中,我已将日期标题值设置为 config.headers.Date 的当前日期,但它在Asp.Net WebApi请求标头类中显示为null。如何设置Date Header值以从客户端获取Local DateTime值。
'use strict';
define(['appconfig'], function (app) {
app.factory('authInterceptorFactory', ['$q', '$location', 'localStorageService', function ($q, $location, localStorageService) {
var authInterceptorServiceFactory = {};
var _request = function (config) {
config.headers = config.headers || {};
// set date
config.headers.Date = new Date().toUTCString();
// set authorization
var authData = localStorageService.get('authorizationData');
if (authData) {
config.headers.Authorization = 'Bearer ' + authData.token;
}
return config;
}
var _responseError = function (rejection) {
if (rejection.status === 401) {
$location.path('/login');
}
return $q.reject(rejection);
}
authInterceptorServiceFactory.request = _request;
authInterceptorServiceFactory.responseError = _responseError;
return authInterceptorServiceFactory;
}]);
});
这是我的WebApi Controller.But ** ApiService.ClientDate **总是返回服务器日期,因为request.Headers.Date的值返回null。请告知如何发送客户端日期时间以从服务器访问。< / p>
public class AuthorizeController : ApiController
{
private ConnectionProvider cp;
protected override void Initialize(System.Web.Http.Controllers.HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
if (User.Identity.IsAuthenticated) {
var request = controllerContext.Request;
// get connection name from claims
ClaimsPrincipal principal = request.GetRequestContext().Principal as ClaimsPrincipal;
var Site = principal.Claims.Where(c => c.Type == "site").Single().Value;
// create db connection
cp = new ConnectionProvider(Site);
ApiService.dbConnection = cp.GetDbConnection();
// create db context
ApiService.dbContext = new Models.WarenetModel(Site);
ApiService.UserId = User.Identity.Name; // set userId
ApiService.HostName = request.Headers.Host; // set hostname
ApiService.ClientDate = request.Headers.Date.HasValue ? request.Headers.Date.Value.LocalDateTime : DateTime.Now; // set client date
}
}
protected override void Dispose(bool disposing)
{
if (disposing) {
if (cp != null) cp.Dispose();
if (ApiService.dbContext != null) ApiService.dbContext.Dispose();
}
base.Dispose(disposing);
}
}