I am trying to implement an extremely simple splash using Nancy as an alternative to ASP.NET MVC.
It must accept the username (without password) and provide meaningful error messages on one login page without requiring an update. If the login was successful, the response contains the URL for the transition.
The POCO for the response is as follows:
public class LoginResponseModel { public bool IsSuccess { get; set; } public string RedirectUrl { get; set; } public string ErrorMessage { get; set; } }
JS handler for login request:
$.ajax({ url: '/login', type: "POST", data: { UserName: username } }).done(function (response) { if (response.IsSuccess) { showSuccess(); document.location.href = response.RedirectUrl; return; } showError(response.ErrorMessage); }).fail(function (msg) { showError("Unable to process login request: " + msg.statusText); });
The problem I am facing is Nancy Forms based authentication. I went through half a dozen different tutorials that are more or less doing the same thing, and have also switched to Nancy authentication demos. The only thing everyone has is that they rely on the LoginAndRedirect extension LoginAndRedirect . I do not want to return a redirect. I want to return the result of a login attempt and let the client handle the navigation.
The IUserMapper implementation that I use:
public class UserMapper : IUserMapper { public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context) {
Relevant part of my LoginModule action:
var result = _userMapper.ValidateUser(input.AccessCode); if (result.Guid != null) this.Login(UserMapper.GUID_ADMIN, expiry); return Response.AsJson(result.Response);
but for subsequent requests, Context.CurrentUser always null.
If I add the following method to the Nancy.Demo.Authentication.Forms sample, it reproduces the behavior that I see in my own project, which leads me to believe that LoginWithoutRedirect is not working as I expected.
Get["/login/{name}"] = x => { Guid? userGuid = UserDatabase.ValidateUser(x.Name, "password"); this.LoginWithoutRedirect(userGuid.Value, DateTime.Now.AddYears(2)); return "Logged in as " + x.Name + " now <a href='~/secure'>see if it worked</a>"; };