/// <summary> /// Checks whether the user is authenticated with a new or used service. /// </summary> private void manageMultipleAccount(Security.User user) { UserID = user.UserId; List <Storage.Service> services = sto.getServicesByUserID(UserID); if (services == null) { showError("An error occurred during recovery services", false); } bool addExt = true; foreach (Storage.Service serv in services) { if (serv.serviceID == servID) { addExt = false; break; } } if (addExt) { /* * Controllo che non esista un altro user (diverso da quello corrente) che abbia questo account esterno. * Se esiste devo fondere i due utenti perche' rappresentano la stessa persona. */ Storage.User userAlredyExist = sto.getUserByExternalAccount(servID, LoginUserName); if (userAlredyExist != null) { showError("External account already exists. UserId=" + userAlredyExist.userID, false); } // Operazione da eseguire su db ora solo se gia' registrato if (user.Registered) { ExternalAccount extAcc = sto.addExternalAccount(UserID, LoginUserName, servID); if (extAcc == null) { showError("An error occurred during the addition of new service", false); } } } }
/// <summary> /// Autenticate a user /// </summary> private void ProcessLoginUser() { string starting = (string)Session["LoginPhase"]; service = (ILoginService)Session["LoginService"]; try { servID = (int)Session["ServiceID"]; } catch (Exception) { servID = -1; } if (string.IsNullOrEmpty(starting)) { #region first phase Storage.Service serv = sto.getEntityByID <Storage.Service>(servID); if (serv == null) { showError("An error occurred while retrieving the service used", false); } string servName = serv.nameService; if (servName == null) { showError("An error occurred while retrieving the service name used", false); } Assembly assembly = null; if (serv.dllPath == null) { assembly = typeof(LoginServices.OAuthBase).Assembly; } else { assembly = Assembly.LoadFrom(serv.dllPath); } foreach (Type type in assembly.GetTypes()) { if (type.IsClass && type.Name == serv.className) { Object instance = Activator.CreateInstance(type); if (instance == null) { service = null; } else { service = (Security.Auth.ILoginService)instance; } } } if (service == null) { showError("An error occurred while creating the service", false); } LoginUrl = service.StartLogin(Session, Server); if (LoginUrl == null) { showError("An error occurred during the first phase of the login.<br />Please check that your time settings are correct.", false); } Session["LoginPhase"] = "Second"; Session["LoginService"] = service; Response.Redirect(LoginUrl); #endregion } else { if (service == null) { showError("An error occurred in the service", false); } if (!starting.Equals("Second")) { showError("An error occurred during the second phase of login", false); } if (!service.ProcessLogin(Session, Request, Server)) { showError("An error occurred during the authentication process", false); } //Process login correctly #region second phase LoginUserName = service.getUsername(); if (LoginUserName == null) { showError("An error occurred in the username", false); } Token tok = (Token)Session["Token"]; Security.User user = null; if ((tok == null) || (!tok.Authenticated)) { #region first time login Storage.User tempUser = sto.getUserByExternalAccount(servID, LoginUserName); if (tempUser != null) { // user already registered UserID = tempUser.userID; user = new Security.User(UserID, tempUser.nickname, tempUser.mail); } else { //user not yet registered UserID = -1; user = new Security.User(UserID, LoginUserName, ""); } user.AddLoggedService(servID, service); tok = new Token(user); Session["Token"] = tok; #endregion } else { #region add service to the current user user = tok.GetCurrentUser(); manageMultipleAccount(user); user.AddLoggedService(servID, service); #endregion } /* clean session */ Session["LoginPhase"] = null; Session["LoginService"] = null; Session["ServiceID"] = null; Session["LoginContact"] = null; Session["LoginError"] = null; string url = (string)Session["ReturnURL"]; if (url == null) { url = URLHome; } Session["ReturnURL"] = null; if (url.Contains("?")) { url += "®=true"; } else { url += "?reg=true"; } Response.Redirect(url); #endregion #region registration //DA CAMBIAREEEEEEEEEEEee /*if (user.Registered) * //redirect to the url where the login process begin * Response.Redirect(url); * else * { * //redirect to registration page * Session["ReturnURL"] = url; * Response.Redirect(URLReg); * }*/ #endregion } }
public Authorization(User _user) { this._user = _user; }