【发布时间】:2016-05-10 10:58:38
【问题描述】:
我有一个带 webapi 后端的角前端。我已经使用 OWIN/Identity 和 JWT 令牌实现了 OAuth v2 安全性(感谢 Taiseer Joudeh 的博客)。我的负担是我们仍然有需要特定 cookie 的遗留页面。当从登录请求返回 JWT 令牌时,我增加了来自 WebApi 的 Http 响应以包含该 cookie。我已验证 cookie 在响应标头中。
我的问题是我无法在我的角度响应处理程序中看到 cookie,我会将其推送到浏览器。根据我在 StackOverflow 其他地方找到的建议,我已经尝试了以下每一项,但到目前为止,我无法在 .js 代码中看到 cookie(替代尝试已被注释掉,但为了完整性而保留)。我还确保通过将“Access-Control-Allow-Headers”添加到“set-cookie”并将“Access-Control-Allow-Credentials”添加到“true”,在服务器上设置了适当的“允许”字段。我的 ValidateClientAuthenticationContext(..) 方法。
我需要做什么才能在我的 webapi 响应中看到附加的 cookie?这是服务器还是客户端的问题?两者都有?
在我的 authService.js 文件中:
var _login = function (loginData) {
// this makes the data "form data"
var data = "grant_type=password&client_id=ngAuthApp&username=" + loginData.userName + "&password=" + loginData.password;
var deferred = $q.defer();
$http.post(serviceBase + 'oauth/token', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
.success(function (response) {
localStorageService.set('authorizationData', { token: response.access_token, userName: loginData.userName });
_authentication.isAuth = true;
_authentication.userName = loginData.userName;
console.log($cookies);
//var xxx = $http.defaults.headers;
//var headers = $http.response.headers;
var ddc = $http.response.cookies;
$cookies.DDC = ddc;
deferred.resolve(response);
})
//.success(function (data, status, headers, config) {
// // any required additional processing here
// var results = [];
// results.data = data;
// results.headers = headers();
// results.status = status;
// results.config = config;
// deferred.resolve(results);
//})
.error(function (err, status) {
_logOut();
deferred.reject(err);
});
return deferred.promise;
};
在我的自定义 OAuthProvider .cs 文件中
公共覆盖任务ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) { // 这里跳过很多代码
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Credentials", new[] { "true" });
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Headers", new[] { "authorization", "content-type", "set-cookie" });
context.Validated();
return Task.FromResult<object>(null);
}
【问题讨论】:
标签: angularjs cookies asp.net-web-api oauth owin