0

我的同事和我自己正在处理一个具有登录功能的申请表包含错误消息(如果有)和一个布尔值,用于说明它是否成功。

在 mvc 4 应用程序级别,以下代码用于将登录详细信息提交到 web api:

登录操作

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(AccountViewModel model)
    {
        if (!ModelState.IsValid) return View("Login", model);

        await _client.PostAsJsonAsync("api/Applicant/CheckApplicant", model)
           .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());

        var service = DependencyResolver.Current.GetService<IApplyService>();

        var loginResult = service.GetLoginResult();

        var loginSuccess = loginResult.LoginSuccess;

        if (loginSuccess != null && (bool) loginSuccess)
        {
            FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);

            return RedirectToRoute("Terms And Conditions");
        }

        return View("Login");
    }

然后通过此方法在 web api 处接收登录详细信息:

检查申请人方法

public String CheckApplicant(Applicant applicant)
    {
        Int32 passwordFailureTimeoutMins = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["PasswordFailureTimeoutMins"]);
        Int32 passwordFailureAttempts = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["PasswordFailureAttempts"]);

        ApplicantRepository applicantRepository = new ApplicantRepository();
        Applicant applicantDB = applicantRepository.GetById(applicant.Email);
        LoginResult loginResult = new LoginResult();
        PasswordHelper passwordHelper = new PasswordHelper();

        if (applicantDB == null)
        {
            loginResult.LoginSuccess = false;
            loginResult.LoginError = "Your password or login may not be correct.";
        }
        else
        {                 
            bool loginFailureCheck;              
            if (applicantDB.LoginFailureCount > passwordFailureAttempts)
            {
                System.TimeSpan diffResult = DateTime.Now.Subtract(Convert.ToDateTime(applicantDB.LastLoginFailure));
                if (diffResult.Minutes < passwordFailureTimeoutMins)
                {
                    loginFailureCheck = false;
                }
                else
                {
                    loginFailureCheck = true;
                }
            }
            else
            {
                loginFailureCheck = true;
            }

            if (passwordHelper.CheckPassword(applicant.Password, applicantDB.Password))
            {
                if(loginFailureCheck)
                {
                    if(applicantDB.AccountActive)
                    {                        
                        loginResult.LoginSuccess = true;
                        loginResult.LoginError = "Login Successful.";
                        applicantDB.LastLoginFailure = null;
                        applicantDB.LastLoginSuccess = DateTime.Now;
                        applicantDB.LoginFailureCount = 0;
                        applicantRepository.Update(applicantDB);                    
                    }
                    else
                    {
                        loginResult.LoginSuccess = false;
                        loginResult.LoginError = "This account has been permanently banned.";
                    }
                }
                else
                {
                    loginResult.LoginSuccess = false;
                    loginResult.LoginError = "This account is now temporarily disabled please wait " + passwordFailureTimeoutMins + " minutes before trying again";
                    applicantDB.LastLoginFailure = DateTime.Now;
                    applicantDB.LoginFailureCount = applicantDB.LoginFailureCount + 1;
                    applicantRepository.Update(applicantDB);
                }
            }
            else
            {
                loginResult.LoginSuccess = false;
                loginResult.LoginError = "Your password or login may not be correct.";
                applicantDB.LastLoginFailure = DateTime.Now;
                applicantDB.LoginFailureCount = applicantDB.LoginFailureCount + 1;
                applicantRepository.Update(applicantDB);
            }
        }
        return JsonConvert.SerializeObject(loginResult);
    }

如您所见,它返回一个 JsonConvert.SerializeObject(loginResult)。

完成此操作后,该过程将返回到上面的 Login ActionResult,然后移动到 GetLoginResult() 方法,如下所示:

获取登录结果

public LoginResult GetLoginResult()
    {
        const string uri = "http://localhost:55830/api/Applicant/CheckApplicant";

        using (var httpClient = new HttpClient())
        {
            var response = httpClient.GetStringAsync(uri);
            return JsonConvert.DeserializeObject<LoginResult>(response.Result);
        }
    }

当它到达这一点时,它返回一个错误 405 方法不允许。

如何在 mvc 4 应用程序级别使用 loginResult 以及从 web api 发送 loginResult 的最佳方式是什么?

任何建议将不胜感激。

4

1 回答 1

0

不确定您到底要做什么,但您是否正在使用 GET 来读取上一个 POST 的结果?您可以阅读 POST 的响应消息以获取结果,如下所示。

public async Task<ActionResult> Login(AccountViewModel model)
{
    if (!ModelState.IsValid) return View("Login", model);

    var message = await _client.PostAsJsonAsync
                       ("api/Applicant/CheckApplicant", model);

    message.EnsureSuccessStatusCode();

    LoginResult result = await message.Content.ReadAsAsync<LoginResult>();

    // do other stuff here
}

将 Web API 操作方法更改为LoginResult直接返回。该框架将为您序列化它。

public LoginResult CheckApplicant(Applicant applicant)
{

}
于 2013-07-19T12:49:38.630 回答