public IActionResult Office365([FromBody] Dictionary <string, string> code) { if (!string.IsNullOrWhiteSpace(code["code"])) { var accessTokenRes = ADAuthUtils.GetAccessToken(code["code"], code["callback"]); if (!accessTokenRes.status) { //return BadRequest("Code is not valid."); return(BadRequest(accessTokenRes.message)); } var claimData = ADAuthUtils.GetClaimData(accessTokenRes.Data.Id_Token); ResponseModel <Office365Model> response = new ResponseModel <Office365Model>(); response = _accountManager.OfficeLoginApi(claimData); if (response.status) { var tokenData = JwtAuthentication.GenerateToken(response.Data.Email); response.Data.Token = tokenData.Data.Token; response.Data.RefreshToken = tokenData.Data.RefreshToken; response.Data.ExpiresIn = tokenData.Data.ExpiresIn; //response.Data.Token = JwtAuthentication.BuildToken(response.Data.Email,response.Data.GivenName); return(Ok(response.Data)); } else { return(BadRequest(response.message)); } } return(BadRequest("Error occurred. Please try again.")); }
public IActionResult Office365SignUp(string code, string callbackurl) { if (!string.IsNullOrWhiteSpace(code)) { var accessTokenRes = ADAuthUtils.GetAccessToken(code, callbackurl); if (!accessTokenRes.status) { return(BadRequest("Code is not valid.")); } var claimData = ADAuthUtils.GetClaimData(accessTokenRes.Data.Id_Token); ResponseModel <Office365Model> response = new ResponseModel <Office365Model>(); response = _accountManager.Office365SignUpApi(claimData); if (response.status) { return(Ok(response.Data)); } else { return(BadRequest(response.message)); } } return(BadRequest("Error occurred. Please try again.")); }
public ActionResult OnAuthComplete(string code) { // HttpContext.Session.SetString( if (string.IsNullOrWhiteSpace(currentContext.HttpContext.Request.Query["code"])) { return(RedirectToLogin("Error occurred!")); } else { var callbackUrl = ADAuthUtils.AppSettings.RedirectUri; var accessTokenRes = ADAuthUtils.GetAccessToken(currentContext.HttpContext.Request.Query["code"], callbackUrl); if (!accessTokenRes.status) { return(RedirectToLogin(accessTokenRes.message)); } var claimData = ADAuthUtils.GetClaimData(accessTokenRes.Data.Id_Token); if (Convert.ToString(currentContext.HttpContext.Session.GetString("LoginType")) == "Login") { var result = _accountManager.Office365Login(claimData); if (result.status) { // currentContext.HttpContext.Session.SetString("LoginType", null); var claims = new List <Claim> { new Claim(ClaimTypes.Name, claimData["email"]), new Claim("FullName", claimData.ContainsKey("given_name")? claimData["given_name"]:claimData["name"]) }; var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); var authProperties = new AuthenticationProperties { }; currentContext.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); currentContext.HttpContext.Session.Remove("LoginType"); return(RedirectToAction("Index", "CreateSession")); } else { ViewBag.ErrorMessage = result.message; return(View("Error")); } } else if (Convert.ToString(currentContext.HttpContext.Session.GetString("LoginType")) == "SignUp") { var result = _accountManager.Office365SignUp(claimData); if (result.status) { // currentContext.HttpContext.Session.SetString("LoginType", null); currentContext.HttpContext.Session.Remove("LoginType"); return(RedirectToAction("Index", "CreateSession")); } else { ViewBag.ErrorMessage = result.message; return(View("Error")); } } else if (Convert.ToString(currentContext.HttpContext.Session.GetString("LoginType")) == "View") { var result = _accountManager.Office365Login(claimData); if (result.status) { //currentContext.HttpContext.Session.SetString("LoginType", null); currentContext.HttpContext.Session.Remove("LoginType"); var url = Convert.ToString(currentContext.HttpContext.Session.GetString("url")); currentContext.HttpContext.Session.Remove("url"); //currentContext.HttpContext.Session.SetString("", null); return(Redirect(url)); } else { return(View("Error")); } } else { return(View("Error")); } } }