我有一个有角度的应用程序,我试图通过REST服务的基本身份验证来验证它。我使用base64中编码的相应的"Base {username:password}“添加授权头,并调用rest,但始终返回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');
}
};
}
]
);这是我的LoginController:
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方法中添加“授权”属性会让主机知道检查授权头中传递的凭据吗?还是我需要实现一些额外的东西,比如在下面这样的文章中(问题部分):basic authentication with custom message handler
在我看来,我的"Get“方法需要更多的内容,但是我找不到任何好的示例来帮助我完成这个过程.(如果您还没有弄清楚,这是我第一次尝试使用基本的身份验证和REST /服务。
编辑:啊哈!我越来越近了。@Chandermani的回应出于某种原因促使我查看了我自己的web.config,我意识到我没有这样的东西:
<security>
<authentication>
<basicAuthentication enabled="true"/>
</authentication>
</security>当导航到./api/登录页面时,添加这一点会提示我输入我的凭据。
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/登录页面提示我输入我的凭据时,它会陷入无限循环。我进去,按回车,然后它又来找他们。
Edit3:好的,我可以认证了!我必须在用户名前面指定域,因为我运行的是localhost.
谢谢大家为我指明正确的方向!
发布于 2014-01-08 14:50:32
改变的组合导致了我的问题的解决:
1)我需要允许在我自己的web.config中进行基本身份验证。我以前只在IISExpress applicationhost.config中设置过它(参见编辑(1))
2)显示Chrome工具的输出,证明了基本身份验证授权头实际上是被发送过来的。
3)最后一个问题是,由于我在本地主机上运行,我需要在我的用户名中指定要验证的域。可能有一种更干净的方法,但我只是在我的用户名字段中输入了这个:mydomain\gander,它就开始工作了。
https://stackoverflow.com/questions/20997841
复制相似问题