当我发送正确的凭据时,我收到HTTP_200_OK响应,一切都按预期工作。但是,当我发送不正确或不完整的登录凭据时,我不会得到任何回复。这是我的代码:
Angular(authService.js登录功能):
$http({
method : 'POST',
url : API_LOCATION + 'login',
data : $.param(credentials),
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function(response){
console.log(response);
if(response.status == 200){
authService.currentUser = response.data;
$location.path('/')
}
else {
$rootScope.alert = response.data.message;
}
});
Django休息框架 - 登录视图:
class LoginView(views.APIView):
serializer_class = UsersSerializer
permission_classes = (AllowAny, )
allowed_methods = ('GET', 'POST', 'OPTIONS', 'HEAD')
def get(self, *args, **kwargs):
serializer = self.serializer_class(self.request.user, context={'request' : self.request})
return Response(serializer.data)
def post(self, *args, **kwargs):
username = self.request.data.get('username', None)
password = self.request.data.get('password', None)
if username and password:
user = authenticate(username=username, password=password)
else:
print '400'
return Response({"message": "Both fields are required"}, status=views.status.HTTP_400_BAD_REQUEST)
if not user:
print '401'
return Response({"message": "Incorrect login"}, status=views.status.HTTP_401_UNAUTHORIZED)
login(self.request, user)
serializer = self.serializer_class(user, context={'request' : self.request})
return Response(serializer.data, status = views.status.HTTP_200_OK)
' 400'和' 401'分别打印在控制台中,但我没有得到任何角度响应。帮助
修改
谢谢pgreen2,得到了它。错误回调函数中收到错误响应:
$http({
method : 'POST',
url : API_LOCATION + 'login',
data : $.param(credentials),
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function successCallback(response) {
if(response.status == 200){
authService.currentUser = response.data;
$location.path('/')
}
}, function errorCallback(response) {
console.log(response)
});
答案 0 :(得分:1)
401和400是错误代码,因此您需要在角度中添加.catch()到您的承诺。错误发生了,但你没有代码处理它。
更新
UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var user = userManager.FindByName(currentUser.LoginName); // currentUser is the currently logged in user
IdentityResult result1 = userManager.RemovePassword(user.Id);
IdentityResult result2 = userManager.AddPassword(user.Id, txtPassword1.Text);