Exemplo n.º 1
0
        public async Task <IActionResult> BackToApp()
        {
            if (User.Identity.IsAuthenticated)
            {
                string accessToken = await HttpContext.GetTokenAsync("access_token");

                OktaHelper oktaHelper = new OktaHelper(_Config);
                var        response   = await oktaHelper.RevokeToken(accessToken);

                foreach (var cookie in Request.Cookies.Keys)
                {
                    Response.Cookies.Delete(cookie);
                }
            }

            string appUrl = dp.UnprotectStr(Request.Query["key"].ToString());

            if (!Uri.IsWellFormedUriString(appUrl, UriKind.Absolute))
            {
                TempData["Message"] = "Sorry something went wrong, please try again!"; //"No valid Url detected to redirect, please initiate the request from the application.";
                return(View("~/Views/Error.cshtml"));
            }
            else
            {
                return(Redirect(appUrl));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Login(LoginModel login)
        {
            string returnUrl = "";
            string path      = dp.UnprotectStr(login.Key);

            if (!Uri.IsWellFormedUriString(path, UriKind.Absolute))
            {
                TempData["Message"] = "Sorry something went wrong, please try again!"; //"No valid Url detected to redirect, please initiate the request from the application.";
                return(View("~/Views/Error.cshtml"));
            }

            if (login.IsOktaSessionExists)
            {
                if (path.Contains("?"))
                {
                    returnUrl = $"{path}&isAuthenticated=true";
                }
                else
                {
                    returnUrl = $"{path}?isAuthenticated=true";
                }

                return(Redirect(returnUrl));
            }
            else
            {
                if (ModelState.IsValid)
                {
                    OktaHelper          oktaHelper = new OktaHelper(_Config);
                    HttpResponseMessage response   = await oktaHelper.Login(login);

                    if (response.IsSuccessStatusCode)
                    {
                        JObject jObj = oktaHelper.GetJsonObject(response);
                        if (jObj["status"] != null)
                        {
                            var status = jObj["status"].Value <string>();
                            switch (status)
                            {
                            case "SUCCESS":
                                var sessionToken = jObj["sessionToken"].Value <string>();

                                if (path.Contains("?"))
                                {
                                    returnUrl = WebUtility.UrlEncode($"{path}&isAuthenticated=true");
                                }
                                else
                                {
                                    returnUrl = WebUtility.UrlEncode($"{path}?isAuthenticated=true");
                                }

                                return(Redirect($"{_Config.Value.Okta_OrgUri}/login/sessionCookieRedirect?token={sessionToken}&redirectUrl={returnUrl}"));

                            case "PASSWORD_EXPIRED":
                                return(ChangePassword(login.Key, jObj["_embedded"]["user"]["id"].Value <string>()));

                            default:
                                break;
                            }
                            ModelState.AddModelError("Error", "Invalid email or password.");
                            return(View($"~/Views/Account/Login.cshtml", login));
                        }
                        else
                        {
                            ModelState.AddModelError("Error", "Invalid email or password.");
                            return(View($"~/Views/Account/Login.cshtml", login));
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Error", "Invalid email or password.");
                        return(View($"~/Views/Account/Login.cshtml", login));
                    }
                }
                else
                {
                    ModelState.AddModelError("Error", "Sorry, we found some errors. Please review the form and make corrections.");
                    return(View($"~/Views/Account/Login.cshtml", login));
                }
            }
        }