コード例 #1
0
        public async Task <ActionResult> Login(LoginViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            string emailConfirmedJson = await ApiService <string> .PostApi($"/api/Account/EmailConfirmed", model.Username);

            string accountDeletedJson = await ApiService <string> .PostApi($"/api/Account/IsDeleted", model.Username);

            if (emailConfirmedJson != null && accountDeletedJson != null)
            {
                bool emailConfirmed = JsonConvert.DeserializeObject <bool>(emailConfirmedJson);
                bool accountDeleted = JsonConvert.DeserializeObject <bool>(accountDeletedJson);

                if (emailConfirmed && !accountDeleted)
                {
                    AuthTokenViewModel result =
                        await ApiService <AuthTokenViewModel> .AuthenticateAsync(model.Username, model.Password,
                                                                                 "/api/Token");

                    if (result != null)
                    {
                        string roles = await ApiService <string> .GetApi("/api/Account/Roles", result.AccessToken);

                        //Keep the user authenticated in the mvc webapp, even when window closes
                        //By using the AccessToken, we can use User.Identity.Name in the MVC controllers to make API calls.
                        CreateTicket(result.AccessToken, roles, model.RememberMe);

                        return(RedirectToAction("Index", "Home"));
                    }
                }
            }

            ModelState.AddModelError("", "Ongeldige login");
            return(View(model));
        }