Example #1
0
        public AuthenticationResponse Login(Objects.AuthenticationRequest request)
        {
            var Response = new AuthenticationResponse();
            try
            {
                if ((request.Username == "") || (request.Password == "") || (request.Domain == ""))
                {
                    throw new Exception("Authentication requests must provide username, password and domain !");
                }
                IntPtr tokenHandle = new IntPtr(0);
                if (LogonUser(request.Username, request.Domain, request.Password, 3, 0, ref tokenHandle))
                {
                    Response.Result = Statics.Result.Success;
                    Response.DeploymentContext.Domain = Properties.Settings.Default.ADDomain;
                    Response.DeploymentContext.DomainController = Properties.Settings.Default.ADDomainController;
                    Response.DeploymentContext.ForceGeneratedName = Properties.Settings.Default.ForceGeneratedComputerNames;

                    // get the user object
                    System.Diagnostics.Debug.WriteLine("Startar PowerShell");
                    var ps = new Powershell.Manager(Properties.Settings.Default.ADDomainController);
                    var psResult = ps.GetUser(request.Username);
                    if (psResult == null)
                        throw new ApplicationException("psResult null");
                    if (psResult.HasErrors())
                    {
                        string errmsg = "";
                        foreach(string str in psResult.Errors)
                             errmsg += str;
                        throw new ApplicationException(errmsg);
                    }

                    System.Diagnostics.Debug.WriteLine("GetUser klar.");
                    var context = new UserContext();
                    context.Username = psResult.ResultAsUser.Username;
                    context.DisplayName = psResult.ResultAsUser.DisplayName;
                    context.Mail = psResult.ResultAsUser.Email;
                    Response.User = context;

                    var deSerializer = new XmlSerializer(typeof(List<OrganizationalUnit>));
                    XmlDocument doc = new XmlDocument();
                    doc.Load(HostingEnvironment.MapPath("~/OrganizationalUnits.xml"));
                    List<OrganizationalUnit> ous = new List<OrganizationalUnit>();
                    foreach (XmlNode ouNode in doc.GetElementsByTagName("OrganizationalUnit"))
                    {
                        // check if user has access
                        bool access = false;
                        foreach (XmlNode userNode in ouNode.SelectNodes("AllowedGroups/Group"))
                        {
                            foreach (string usergrp in psResult.ResultAsUser.MemberOf)
                            {
                                if (userNode.InnerText.ToLower().Trim() == usergrp.ToLower().Trim())
                                {
                                    var ou = new OrganizationalUnit();
                                    ou.DisplayName = ouNode.SelectNodes("DisplayName")[0].InnerText;
                                    ou.DistinguishedName = ouNode.SelectNodes("DistinguishedName")[0].InnerText;
                                    foreach (XmlNode prefixNode in ouNode.SelectNodes("ComputerNamePrefixes/Prefix"))
                                    {
                                        ou.ComputerNamePrefixes.Add(prefixNode.InnerText);
                                    }
                                    foreach (XmlNode groupNode in ouNode.SelectNodes("Groups/Group"))
                                    {
                                        ou.Groups.Add(groupNode.InnerText);
                                    }
                                    ous.Add(ou);
                                    access = true;
                                    break;
                                }
                            }
                            if (access)
                                break;
                        }
                    }
                    Response.DeploymentContext.OrganizationalUnits = ous;
                }
                else
                {
                    throw new Exception("Login failed !");
                }
            }
            catch (Exception ex)
            {
                Response.Result = Statics.Result.Error;
                Response.Errors.Add(ex.Message + ex.StackTrace);
            }
            return Response;
        }
Example #2
0
 public OperationResult GetUser(string username)
 {
     var ps = new Powershell.Manager(Properties.Settings.Default.ADDomainController);
     var result = ps.GetUser(username);
     return result;
 }