protected void Unnamed_LoggingOut(object sender, LoginCancelEventArgs e) { Session.Clear(); Session.Abandon(); var identity = (ClaimsIdentity)HttpContext.Current.User.Identity; var identityProvider = identity.FindFirst(ClaimTypes.System); string email = identity.FindFirst(ClaimTypes.Name).Value; Context.GetOwinContext().Authentication.SignOut(); if (identityProvider != null) { if (identityProvider.Value == AcquisitionEnum.Office365.ToString()) { LogoutRequest logoutReq = new LogoutRequest(); JObject jsonObj = ConfigService.GetCompanyConfigJsonByHostName(Request.Url.Host, Configuration.OFFICE365_CODE); logoutReq.Issuer = jsonObj["appID"].ToString(); logoutReq.NameID = email; string endPoint = jsonObj["endPoint"].ToString(); StringWriter sw = new StringWriter(); XmlTextWriter tw = null; try { XmlSerializer serializer = new XmlSerializer(logoutReq.GetType()); tw = new XmlTextWriter(sw); serializer.Serialize(tw, logoutReq); } catch (Exception ex) { } finally { sw.Close(); if (tw != null) { tw.Close(); } } Response.Redirect(string.Format("{0}?SAMLRequest={1}", endPoint, SamlUtil.EncodeRequest(sw.ToString()))); } if (identityProvider.Value == AcquisitionEnum.Adfs.ToString()) { } } }
protected void Page_Load(object sender, EventArgs e) { string samlResponse = Request.Form["SAMLResponse"]; logger.Info(samlResponse); if (!string.IsNullOrEmpty(samlResponse)) { string decodedResponse = SamlUtil.DecodeResponse(samlResponse); if (Request.IsAuthenticated) { Request.GetOwinContext().Authentication.SignOut(); } IAuthenticate auth = new AzureAuthenticate(UnitOfWork, Request.Url.Host, decodedResponse); var user = AuthService.SignIn(auth); logger.Debug("user: "******"start claim settings...{0} - {1}", user.Id, user.Email); var identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie); identity.AddClaim(new Claim(ClaimTypes.Name, user.Id)); identity.AddClaim(new Claim(ClaimTypes.Email, user.Email)); identity.AddClaim(new Claim(ClaimTypes.Role, string.Join("\t", user.UserGroups.Select(x => x.GroupId)))); identity.AddClaim(new Claim(ClaimTypes.Uri, Request.Url.Host)); identity.AddClaim(new Claim(ClaimTypes.System, AcquisitionEnum.Office365.ToString())); Request.GetOwinContext().Authentication.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity); if (!string.IsNullOrEmpty(Request.QueryString["ReturnUrl"])) { Request.GetOwinContext().Response.Redirect(Request.QueryString["ReturnUrl"]); } else { Request.GetOwinContext().Response.Redirect("/Default"); } } } else { logger.Trace("Azure login..."); AuthnRequest authnReq = new AuthnRequest(); var config = UnitOfWork.ConfigurationRepository.FindAsNoTracking(x => x.Code == Configuration.OFFICE365_CODE).FirstOrDefault(); var company = CompanyService.GetCompanyByHostName(Request.Url.Host); logger.Trace("config: {0}, company: {1}", config, company); if (config == null || company == null) { return; } var companyConfig = company.CompanyConfigurations.First(x => x.ConfigurationId == config.Id); var jsonObj = JObject.Parse(companyConfig.ConfigJson); var issuer = jsonObj["appID"].ToString(); var endPoint = jsonObj["endPoint"].ToString(); authnReq.Issuer = issuer; StringWriter sw = new StringWriter(); XmlTextWriter tw = null; try { XmlSerializer serializer = new XmlSerializer(authnReq.GetType()); tw = new XmlTextWriter(sw); serializer.Serialize(tw, authnReq); } catch (Exception ex) { //Handle Exception Code logger.Error(ex.ToString()); } finally { sw.Close(); if (tw != null) { tw.Close(); } } Response.Redirect(string.Format("{0}?SAMLRequest={1}", endPoint, SamlUtil.EncodeRequest(sw.ToString()))); } }