예제 #1
0
 /// <summary>
 /// Converts UTC time to member's local time.
 /// </summary>
 /// <param name="Date">Member's UTC time.</param>
 /// <returns></returns>
 public static DateTime ToLocalTime(DateTime Date)
 {
     if (Date != DateTime.MinValue)
     {
         if (AppSession.Profile != null && AppSession.Profile.Member != null && AppSession.Profile.Member.TimeZoneID != null && AppSession.Profile.Member.TimeZoneID.Length > 0)
         {
             try
             {
                 TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(AppSession.Profile.Member.TimeZoneID);
                 return(TimeZoneInfo.ConvertTimeFromUtc(Date, timeZoneInfo));
             }
             catch (Exception ex)
             {
                 AuditEvent.AppEventError(AppSession.Parameters.GeneralAdminEmail.Value, String.Format("Wrong member time zone name:{0}", AppSession.Profile.Member.TimeZoneID), String.Format("Wrong member time zone name: {0} Message:{1} ", AppSession.Profile.Member.TimeZoneID, ex.Message), true);
                 return(TimeZone.CurrentTimeZone.ToLocalTime(Date));
             }
         }
         else
         {
             return(TimeZone.CurrentTimeZone.ToLocalTime(Date));
         }
     }
     else
     {
         return(Date);
     }
 }
예제 #2
0
        /// <summary>
        /// Returns LDAP member info which are stored in the directory.
        /// </summary>
        /// <returns></returns>
        public LDAPMemberInfo GetMemberInfo()
        {
            DirectoryEntry DirEntry = new DirectoryEntry();
            {
                DirEntry.Path               = DirectoryPath;
                DirEntry.Username           = UserName;
                DirEntry.Password           = Password;
                DirEntry.AuthenticationType = AuthenticationTypes.Secure;
            }

            LDAPMemberInfo memberInfo = new LDAPMemberInfo();

            string[] nameParts = UserName.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries);

            DirectorySearcher search = new DirectorySearcher(DirEntry);

            search.Filter = "(SAMAccountName=" + nameParts[0] + ")";
            search.PropertiesToLoad.Add("cn");
            search.PropertiesToLoad.Add("mail");
            try{
                SearchResult result = search.FindOne();
                if (null != result)
                {
                    memberInfo.DisplayName = (String)result.Properties["cn"][0];
                    memberInfo.Email       = (String)result.Properties["mail"][0];
                }
            }
            catch (Exception ex)
            {
                String Message = String.Format("Member:{0}, Message:{1}", UserName, ex.Message);
                AuditEvent.AppEventError(AppSession.Parameters.GeneralAdminEmail.Value, "LDAP obtaining member name failed. " + Message, AuditEvent.GetSessionDetails(Message), true);
            }

            return(memberInfo);
        }
예제 #3
0
        /// <summary>
        /// Authenticates a member in LDAP directory.
        /// </summary>
        /// <returns>True if Aathentication was fine.</returns>
        public bool Authenticate()
        {
            bool authentic = false;

            try
            {
                DirectoryEntry entry        = new DirectoryEntry(DirectoryPath, UserName, Password);
                object         nativeObject = entry.NativeObject;

                string[] nameParts = UserName.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries);

                DirectorySearcher search = new DirectorySearcher(entry);
                search.Filter = "(SAMAccountName=" + nameParts[0] + ")";
                search.PropertiesToLoad.Add("cn");
                search.PropertiesToLoad.Add("mail");
                SearchResult result = search.FindOne();

                if (null != result)
                {
                    MemberInfo.DisplayName = (String)result.Properties["cn"][0];
                    MemberInfo.Email       = (String)result.Properties["mail"][0];
                }

                authentic = true;
            }
            catch (DirectoryServicesCOMException ex) {
                String Message = String.Format("Member: {0}  Message:{1}", UserName, ex.Message);
                AuditEvent.AppEventError(AppSession.Parameters.GeneralAdminEmail.Value, "LDAP authentication failed. " + Message, AuditEvent.GetSessionDetails(Message), true);
            }

            return(authentic);
        }
예제 #4
0
        /// <summary>
        /// Returns LDAP directory member groups divided by | symbol.
        /// </summary>
        /// <returns>Member groups string divided by | symbol.</returns>
        public string GetGroups()
        {
            DirectoryEntry DirEntry = new DirectoryEntry();

            {
                DirEntry.Path               = DirectoryPath;
                DirEntry.Username           = UserName;
                DirEntry.Password           = Password;
                DirEntry.AuthenticationType = AuthenticationTypes.Secure;
            }

            string[] nameParts = UserName.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries);

            DirectorySearcher search = new DirectorySearcher(DirEntry);

            search.Filter = "(SAMAccountName=" + nameParts[0] + ")";
            search.PropertiesToLoad.Add("memberOf");
            StringBuilder groupNames = new StringBuilder();

            try
            {
                SearchResult result        = search.FindOne();
                int          propertyCount = result.Properties["memberOf"].Count;
                String       dn;
                int          equalsIndex, commaIndex;

                for (int propertyCounter = 0; propertyCounter < propertyCount;
                     propertyCounter++)
                {
                    dn = (String)result.Properties["memberOf"][propertyCounter];

                    equalsIndex = dn.IndexOf("=", 1);
                    commaIndex  = dn.IndexOf(",", 1);

                    if (-1 == equalsIndex)
                    {
                        return(null);
                    }

                    groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
                    groupNames.Append("|");
                }
            }
            catch (Exception ex)
            {
                AuditEvent.AppEventError(AppSession.Parameters.GeneralAdminEmail.Value, "LDAP obtaining group names failed.", String.Format("Member:{0}, Message:{1}", UserName, ex.Message), true);
            }
            return(groupNames.ToString());
        }
예제 #5
0
        protected void Application_Error()
        {
            var exception     = Server.GetLastError();
            var httpException = exception as HttpException;

            string Message = exception.Message;
            string Details = String.Format("Source: {0}\r\nStackTrace: {1}", exception.Source, exception.StackTrace);

            if (exception.InnerException != null)
            {
                Message += " " + exception.InnerException.Message;
                Details += String.Format("\r\nSource: {0}\r\nStackTrace: {1}", exception.InnerException.Source, exception.InnerException.StackTrace);
            }


            try
            {
                AuditEvent.AppEventError(AppSession.Parameters.GeneralAdminEmail.Value, "Unhandled exception: " + Message, AuditEvent.GetSessionDetails(Details));
            }
            catch
            {
            }

            if (HttpContext.Current.IsDebuggingEnabled || (User.Identity.IsAuthenticated && AppSession.IsMemberInAdminRole))
            {
            }
            else
            {
                Response.Clear();
                Server.ClearError();

                var routeData = new RouteData();
                routeData.Values["controller"] = "Errors";
                routeData.Values["action"]     = "General";
                routeData.Values["exception"]  = exception;
                Response.StatusCode            = 500;

                if (httpException != null)
                {
                    Response.StatusCode = httpException.GetHttpCode();
                    switch (Response.StatusCode)
                    {
                    case 403:
                        routeData.Values["action"] = "Http403";
                        break;

                    case 404:
                        routeData.Values["action"] = "Http404";
                        break;
                    }
                }

                // Avoid IIS7 getting in the middle
                // http://blog.davebouwman.com/2011/04/21/custom-404-pages-for-asp-net-mvc-3/
                Response.TrySkipIisCustomErrors = true;

                IController errorsController = new ErrorsController();
                var         rc = new RequestContext(new HttpContextWrapper(Context), routeData);
                errorsController.Execute(rc);
            }
        }
예제 #6
0
        public ActionResult ImportRoles()
        {
            System.Threading.Thread.Sleep(200);
            CultureInfo provider = CultureInfo.InvariantCulture;

            List <Role> RoleResult = new List <Role>();

            try
            {
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    System.Threading.Thread.Sleep(1);

                    String             FileContent = "";
                    HttpPostedFileBase PostedFile  = Request.Files[i];

                    using (System.IO.Stream input = PostedFile.InputStream)
                    {
                        byte[] buffer = new byte[PostedFile.InputStream.Length];
                        input.Read(buffer, 0, buffer.Length);
                        FileContent = System.Text.ASCIIEncoding.UTF8.GetString(buffer);
                    }


                    if (FileContent.Length > 0)
                    {
                        string[] Lines = FileContent.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                        AuditEvent.AppEventSuccess(Profile.Member.Email, String.Format(AuditEvent.RoleUploadHasStarted, Lines.Length));

                        int LineNumber = 1;
                        foreach (String Line in Lines)
                        {
                            if (LineNumber == 1 || Line == null || Line.Trim().Length == 0)
                            {
                                LineNumber++;
                                continue;
                            }

                            List <string> Values = new List <string>();
                            CsvParser     parser = new CsvParser(Line);

                            foreach (string s in parser)
                            {
                                Values.Add(s.Trim());
                            }

                            if (Values.Count >= 4)
                            {
                                Role Role = Web.Admin.Logic.Collections.Roles.GetBy(Values[0]);
                                if (Role.RoleID > 0)
                                {
                                    Role.UIMessage     = "Role already exists.";
                                    Role.UILineNumber  = LineNumber;
                                    Role.UIMessageType = Logic.UIMessageType.Warning;
                                    AuditEvent.AppEventWarning(Profile.Member.Email, String.Format(AuditEvent.RoleUploadMemberExists, Role.Name));
                                }
                                else
                                {
                                    Role.Name     = Values[0];
                                    Role.Settings = Values[1];

                                    if (Values[3].Trim().Length == 0)
                                    {
                                        Role.BackColor = "aaaaaa";
                                    }
                                    else
                                    {
                                        Role.BackColor = Values[2];
                                    }

                                    if (Values[3].Trim().Length == 0)
                                    {
                                        Role.ForeColor = "000000";
                                    }
                                    else
                                    {
                                        Role.ForeColor = Values[3];
                                    }

                                    if (BuiltInRoleMember.ContainsRole(Role.Name))
                                    {
                                        Role.IsBuiltIn = true;
                                    }
                                    else
                                    {
                                        Role.IsBuiltIn = false;
                                    }

                                    try
                                    {
                                        Role.Created = DateTime.ParseExact(Values[4], "dd.MM.yyyy HH:mm:ss", provider);
                                    }
                                    catch { }

                                    try
                                    {
                                        Role.Modified = DateTime.ParseExact(Values[5], "dd.MM.yyyy HH:mm:ss", provider);
                                    }
                                    catch {
                                    }

                                    Role.Save();

                                    Role.UIMessage     = "Role has been added.";
                                    Role.UIMessageType = Logic.UIMessageType.Success;
                                    Role.UILineNumber  = LineNumber;
                                    AuditEvent.AppEventSuccess(Profile.Member.Email, String.Format(AuditEvent.RoleUploadedItemAdded, Role.Name));
                                }

                                RoleResult.Add(Role);
                            }
                            else if (Values.Count > 0)
                            {
                                Role Role = new Role();

                                Role.UIMessage     = "Wrong line format. Number of columns: " + Values.Count.ToString();
                                Role.UIMessageType = Logic.UIMessageType.ErrorOrDanger;
                                Role.UILineNumber  = LineNumber;
                                AuditEvent.AppEventSuccess(Profile.Member.Email, String.Format(AuditEvent.RoleUploadWrongLineFormat, LineNumber, Values.Count));

                                RoleResult.Add(Role);
                            }

                            LineNumber++;
                        }
                    }
                }

                System.Threading.Thread.Sleep(1000);
                RoleListModel Model = new RoleListModel();
                Model.Roles = RoleResult;

                return(Json(new
                {
                    NotifyType = NotifyType.Dialog,
                    Html = this.RenderPartialView(@"_ImportRolesResult", Model)
                }, "text/html", JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                RequestResultModel _model = new RequestResultModel();

                _model.Title         = "Warning";
                _model.Message       = String.Format("Something went wrong: {0}", HttpUtility.HtmlEncode(ex.Message));
                _model.InfoType      = RequestResultInfoType.ErrorOrDanger;
                _model.HideInSeconds = 0;
                _model.Height        = 250;

                AuditEvent.AppEventError(Profile.Member.Email, _model.Message, AuditEvent.GetSessionDetails(ex.StackTrace));

                return(Json(new
                {
                    NotifyType = NotifyType.DialogInline,
                    Html = this.RenderPartialView(@"_RequestResultDialog", _model)
                }, "text/html", JsonRequestBehavior.AllowGet));
            }
        }