using System.Collections.Specialized;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
namespace CPE.App.Web.Connect {
public class Login {
///
/// Logs into the Connect service with the information provided and returns the Session object containing the session key.
///
/// The username/login of the account.
/// The password for the account.
/// The URL to the connect service.
/// The primary key of the Connect account/host.
public static Session UserLogin(string username, string password, string connectURL, int? accountId) {
// Attempt login
var request = new Request(connectURL, "login");
request.Parameters.Add("login", username);
request.Parameters.Add("password", password);
if(accountId.HasValue)
request.Parameters.Add("account-id", accountId.Value.ToString());
// Check if valid
if(request.Execute() && request.Status == Status.OK && !string.IsNullOrEmpty(request.ExtractSessionKey())) {
return new Session() {
Hostname = connectURL,
SessionKey = request.ExtractSessionKey()
};
}
return null;
}
///
/// Resets the password for the specified account.
///
/// The administrative session.
/// The principal-id of the user's account.
/// The new password.
public static bool ResetPassword(Session admin, int userid, string password) {
var request = new Request(admin, "user-update-pwd");
request.Parameters.Add("user-id", userid.ToString());
request.Parameters.Add("password", password);
request.Parameters.Add("password-verify", password);
return (request.Execute() && request.Status == Status.OK);
}
///
/// Creates a new guest user with the specified information.
///
/// The administrative session.
/// The login/username of the user.
/// The password for the user.
/// The e-mail for the user.
/// The user's full name.
/// Returns the new principal-id of the user.
public static int CreateGuest(Session admin, string username, string password, string email, string firstName, string lastName) {
var request = new Request(admin, "principal-update");
request.Parameters.Add("type", "guest");
request.Parameters.Add("has-children", "false");
request.Parameters.Add("login", username);
request.Parameters.Add("password", password);
request.Parameters.Add("email", email);
request.Parameters.Add("first-name", firstName);
request.Parameters.Add("last-name", lastName);
if(request.Execute() && request.Status == Status.OK) {
XmlNode principal = request.XmlResults.SelectSingleNode("/results/principal");
if(principal != null)
return int.Parse(principal.Attributes["principal-id"].Value);
}
return 0;
}
}
}