【发布时间】:2014-01-08 14:03:09
【问题描述】:
我有一个 Angular 应用程序,我正在尝试使用基本身份验证对我的 REST 服务进行身份验证。我正在添加带有以 base64 编码的相应“Base {username:password}”的授权标头,我正在调用我的 rest api,但不断返回 401。我显然在这里错过了一步......
这是角码:
angular
.module('myApp.services')
.factory('AuthenticationService', ['$http', '$q', '$location', 'Base64', 'SessionService', function ($http, $q, $location, encoder, session) {
return {
authenticate:
function (user, password) {
var deferred = $q.defer();
var url = "http://localhost:28924/api/login";
if (user && password) {
var encoded = encoder.encode(user + ':' + password);
$http.defaults.headers.common.Authorization = 'Basic ' + encoded;
console.log('here');
sessionStorage.setItem('Authorization', $http.defaults.headers.common.Authorization);
$http.get(url)
.success(function (data, status, headers, config) {
console.log('login Successful in Authentication service');
deferred.resolve(data);
session.setSession();
})
.error(function (data, status, headers, config) {
deferred.reject(status);
});
}
else {
deferred.reject(401);
}
return deferred.promise;
},
logout:
function () {
$http.defaults.headers.common.Authorization = null;
$http.defaults.headers.common.Impersonate = null;
$location.url('/login');
}
};
}
]
);
这是我的登录控制器:
public class LoginController : ApiController
{
public LoginController()
{
}
[Authorize]
public HttpResponseMessage Get()
{
//return this.ControllerContext.Request.CreateResponse(HttpStatusCode.OK);
if (this.User.Identity.IsAuthenticated)
{
return this.ControllerContext.Request.CreateResponse(HttpStatusCode.OK);
}
return this.ControllerContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
}
}
我还将 IISExpress 配置文件设置为“允许”基本身份验证,如下所述:set basic authentication in IIS Express。
是否只是将“授权”属性添加到我的 Get 方法让主机知道检查在授权标头中传递的凭据?还是我需要实现一些额外的东西,比如这个 SO 帖子(问题部分):basic authentication with custom message handler?
在我看来,我的“Get”方法需要更多内容,但我找不到任何好的例子来帮助我完成这个......(如果你还没有弄清楚,这个是我第一次尝试基本身份验证和 REST api/服务。
编辑:啊哈!我越来越近了。 @Chandermani 出于某种原因的回应促使我检查了我的 OWN web.config,我意识到我没有这个:
<security>
<authentication>
<basicAuthentication enabled="true"/>
</authentication>
</security>
现在添加它会在导航到 .../api/login 页面时提示我输入我的凭据。
Edit2: 检查通过 Chrome 开发工具通过网络发送的内容表明正在发送授权标头,同时指定基本身份验证和我的 base64 编码用户名和密码。这是它的样子:
Request URL:http://localhost:28924/api/login
Request Method:GET
Status Code:401 Unauthorized
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Authorization:Basic YWblahblahblahDE=
Connection:keep-alive
Host:localhost:28924
Referer:http://localhost:28924/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Response Headersview source
Cache-Control:private
Content-Length:6333
Content-Type:text/html; charset=utf-8
Date:Wed, 08 Jan 2014 14:32:14 GMT
Server:Microsoft-IIS/8.0
WWW-Authenticate:Negotiate
WWW-Authenticate:NTLM
WWW-Authenticate:Basic realm="localhost"
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?QzpcU291cmNlQ29kZVxUcmlhbHNNb2JpbGVcVHJhaWxzTW9iaWxlLldlYlxhcGlcbG9naW4=?=
当 api/login 页面提示我输入凭据时,它会陷入无限循环。我将输入它们,按 Enter,然后它会再次询问它们。
Edit3:好的,我可以进行身份验证了!!!我必须在我的用户名前面指定域,因为我正在运行 localhost...
感谢大家为我指明正确的方向!
【问题讨论】:
-
首先尝试从浏览器直接访问 api。是否提示输入用户名\密码?如果确实如此,然后显示服务配置正确的内容。然后你可以看到 Angular 发送了什么。您可以使用 fiddler 来跟踪正在发出的请求。
-
@Chandermani 在不输入我的凭据的情况下直接导航到该页面时,会显示以下内容:{"Message":"Authorization has been denied for this request."}
-
它会弹出一个浏览器对话框来输入用户名密码吗?
-
在`system.web/authentication'中将认证模式设置为
Windows怎么样? -
@Chandermani 是的,(浏览器对话框),请在最后查看我的问题中的编辑...
标签: c# asp.net angularjs rest basic-authentication