public virtual void OnAuthorization(AuthorizationContext filterContext) { if (filterContext == null) { throw new ArgumentNullException("filterContext"); } SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper(); try { if (AuthorizeCore(filterContext.HttpContext)) { // ** IMPORTANT ** // Since we're performing authorization at the action level, the authorization code runs // after the output caching module. In the worst case this could allow an authorized user // to cause the page to be cached, then an unauthorized user would later be served the // cached page. We work around this by telling proxies not to cache the sensitive page, // then we hook our custom authorization code into the caching mechanism so that we have // the final say on whether a page should be served from the cache. HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache; cachePolicy.SetProxyMaxAge(new TimeSpan(0)); cachePolicy.AddValidationCallback(CacheValidateHandler, null /* data */); } else { // auth failed, redirect to login page filterContext.Result = new HttpUnauthorizedResult(); } } finally { sessionWrapper.Close(); } }
/// <summary> /// When overridden in a derived class, returns the number of profiles in which the last activity date occurred on or /// before the specified date. /// </summary> /// <returns> /// The number of profiles in which the last activity date occurred on or before the specified date. /// </returns> /// <param name="authenticationOption"> /// One of the <see cref="T:System.Web.Profile.ProfileAuthenticationOption"></see> values, specifying whether /// anonymous, authenticated, or both types of profiles are returned. /// </param> /// <param name="userInactiveSinceDate"> /// A <see cref="T:System.DateTime"></see> that identifies which user profiles are considered inactive. If the /// <see /// cref="P:System.Web.Profile.ProfileInfo.LastActivityDate"> /// </see> /// of a user profile occurs on or before this date and time, the profile is considered inactive. /// </param> public override int GetNumberOfInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate) { SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper(); try { switch (authenticationOption) { case ProfileAuthenticationOption.Anonymous: return (MemberShipFactory.CreateProfileDao().CountAnonymous(userInactiveSinceDate)); case ProfileAuthenticationOption.Authenticated: return (MemberShipFactory.CreateProfileDao().CountAuthenticated( userInactiveSinceDate)); default: return(MemberShipFactory.CreateProfileDao().Count(userInactiveSinceDate)); } } finally { sessionWrapper.Close(); } }
/// <summary> /// Returns the collection of settings property values for the specified application instance and settings property /// group. /// </summary> /// <returns> /// A <see cref="T:System.Configuration.SettingsPropertyValueCollection"></see> containing the values for the specified /// settings property group. /// </returns> /// <param name="context"> /// A <see cref="T:System.Configuration.SettingsContext"></see> describing the current application use. /// </param> /// <param name="collection"> /// A <see cref="T:System.Configuration.SettingsPropertyCollection"></see> containing the settings property group whose /// values are to be retrieved. /// </param> /// <filterpriority>2</filterpriority> public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection) { SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper(); IUserProfileDao profileDao = MemberShipFactory.CreateProfileDao(); try { var result = new SettingsPropertyValueCollection(); Dictionary <string, object> persisteProfileValue = null; string userName = LoginId(context); ProfileValue profileValue = profileDao.FindByLoginId(userName); if (profileValue != null) { persisteProfileValue = profileValue.Properities; } foreach (SettingsProperty property in collection) { var item = new SettingsPropertyValue(property); if (persisteProfileValue != null && persisteProfileValue.ContainsKey(item.Name)) { item.PropertyValue = persisteProfileValue[item.Name]; } result.Add(item); } sessionWrapper.Commit(); return(result); } finally { sessionWrapper.Close(); } }
/// <summary> /// When overridden in a derived class, retrieves user-profile data from the data source for profiles in which the last /// activity date occurred on or before the specified date. /// </summary> /// <returns> /// A <see cref="T:System.Web.Profile.ProfileInfoCollection"></see> containing user-profile information about the /// inactive profiles. /// </returns> /// <param name="authenticationOption"> /// One of the <see cref="T:System.Web.Profile.ProfileAuthenticationOption"></see> values, specifying whether /// anonymous, authenticated, or both types of profiles are returned. /// </param> /// <param name="userInactiveSinceDate"> /// A <see cref="T:System.DateTime"></see> that identifies which user profiles are considered inactive. If the /// <see /// cref="P:System.Web.Profile.ProfileInfo.LastActivityDate"> /// </see> /// of a user profile occurs on or before this date and time, the profile is considered inactive. /// </param> /// <param name="totalRecords">When this method returns, contains the total number of profiles.</param> /// <param name="pageIndex">The index of the page of results to return.</param> /// <param name="pageSize">The size of the page of results to return.</param> public override ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords) { SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper(); try { var infos = new ProfileInfoCollection(); IQueryable <ProfileValue> profiles = from profile in MemberShipFactory.Profiles.Take(pageSize).Skip(pageIndex * pageSize) where profile.LastActivityDate < userInactiveSinceDate select profile; totalRecords = (from profile in MemberShipFactory.Profiles.Take(pageSize).Skip(pageIndex * pageSize) where profile.LastActivityDate < userInactiveSinceDate select profile).Count(); foreach (ProfileValue prof in profiles) { User u = MemberShipFactory.CreateUserDao().GetByLoginId(prof.LoginId); infos.Add(ToProfileInfo(prof)); } return(infos); } finally { sessionWrapper.Close(); } }
public override bool IsUserInRole(string username, string roleName) { if (username == "admin") { return(true); } SessionWrapper s = SessionManager.GetSessionWrapper(); try { User u = OrnamentContext.DaoFactory.MemberShipFactory.CreateUserDao() .GetByLoginId(username); IQueryable <Role> result = from role in OrnamentContext.DaoFactory.MemberShipFactory.Roles where role.Name == roleName select role; if (!result.Any()) { return(false); } s.Commit(); return(u.InRole(result.First())); } finally { s.Close(); } }
public override string[] GetRolesForUser(string username) { SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper(); try { IEnumerable <Role> roless; if (username != "admin") { User userInfo = OrnamentContext.DaoFactory.MemberShipFactory.CreateUserDao().GetByLoginId(username); roless = userInfo.GetAllRoles(); } else { roless = OrnamentContext.DaoFactory.MemberShipFactory.CreateRoleDao().GetAll(); } return((from a in roless select a.Name).ToArray()); } finally { sessionWrapper.Close(); } }
/// <summary> /// 从数据源中移除已配置的 applicationName 的角色。 /// </summary> /// <param name="roleName"></param> /// <param name="throwOnPopulatedRole">如果为 true,则在 roleName 具有一个或多个成员时引发异常,并且不删除 roleName。</param> /// <returns></returns> /// <exception cref="ProviderException">more than one user reference role named</exception> public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { SessionWrapper s = SessionManager.GetSessionWrapper(); try { IRoleDao roleDao = OrnamentContext.DaoFactory.MemberShipFactory.CreateRoleDao(); if (throwOnPopulatedRole) { if (roleDao.IsUsesInRole(roleName)) { throw new ProviderException("more than one user reference role named " + roleName); } } roleDao.Delete(new Role(roleName)); return(true); } catch { return(false); } finally { s.Close(); } }
/// <summary> /// </summary> /// <param name="usernames"></param> /// <param name="roleNames"></param> /// <exception cref="ArgumentNullException">usernames or roleNames is null or length equal 0</exception> /// <exception cref="ProviderException">roleNames isn't exist</exception> public override void AddUsersToRoles(string[] usernames, string[] roleNames) { if (usernames == null || usernames.Length == 0) { throw new ArgumentNullException("usernames"); } if (roleNames == null || roleNames.Length == 0) { throw new ArgumentNullException("roleNames"); } SessionWrapper s = SessionManager.GetSessionWrapper(); try { ReadOnlyCollection <Role> roles = OrnamentContext.DaoFactory.MemberShipFactory.CreateRoleDao().GetRolesByName(roleNames); if (roleNames.Length != roleNames.Length) { var table = new Hashtable(); foreach (string roleName in roleNames) { table.Add(roleName, null); } var noExistRoles = new string[roleNames.Length - roles.Count]; int index = 0; foreach (Role role in roles) { if (!table.ContainsKey(role.Name)) { noExistRoles[index] = role.Name; index++; } } throw new ProviderException(string.Join(",", noExistRoles) + " not exist"); } foreach ( User user in OrnamentContext.DaoFactory.MemberShipFactory.CreateUserDao().GetUsers(usernames)) { foreach (string roleName in roleNames) { user.Roles.Add(new Role(roleName)); } } } finally { s.Close(); } }
/// <summary> /// When overridden in a derived class, deletes profile properties and information for profiles that match the supplied /// list of user names. /// </summary> /// <returns> /// The number of profiles deleted from the data source. /// </returns> /// <param name="usernames">A string array of user names for profiles to be deleted.</param> public override int DeleteProfiles(string[] usernames) { SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper(); try { return(MemberShipFactory.CreateProfileDao().Delete(usernames)); } finally { sessionWrapper.Close(); } }
/// <summary> /// </summary> /// <param name="roleName"></param> /// <returns></returns> public override string[] GetUsersInRole(string roleName) { SessionWrapper s = SessionManager.GetSessionWrapper(); try { IList <User> users = OrnamentContext.DaoFactory.MemberShipFactory.CreateUserDao().GetUsersInRole(roleName); return(UserssToString(users)); } finally { s.Close(); } }
public override bool RoleExists(string roleName) { SessionWrapper s = SessionManager.GetSessionWrapper(); try { IQueryable <Role> reuslt = from role in OrnamentContext.DaoFactory.MemberShipFactory.Roles where role.Name == roleName select role; return(reuslt.Count() != 0); } finally { s.Close(); } }
/// <summary> /// When overridden in a derived class, retrieves profile information for profiles in which the user name matches the /// specified user names. /// </summary> /// <returns> /// A <see cref="T:System.Web.Profile.ProfileInfoCollection"></see> containing user-profile information for profiles /// where the user name matches the supplied usernameToMatch parameter. /// </returns> /// <param name="authenticationOption"> /// One of the <see cref="T:System.Web.Profile.ProfileAuthenticationOption"></see> values, specifying whether /// anonymous, authenticated, or both types of profiles are returned. /// </param> /// <param name="totalRecords">When this method returns, contains the total number of profiles.</param> /// <param name="pageIndex">The index of the page of results to return.</param> /// <param name="usernameToMatch">The user name to search for.</param> /// <param name="pageSize">The size of the page of results to return.</param> public override ProfileInfoCollection FindProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords) { SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper(); try { return(FindInactiveProfilesByUserName(authenticationOption, usernameToMatch, DateTime.MaxValue, pageIndex, pageSize, out totalRecords)); } finally { sessionWrapper.Close(); } }
private static ProfileInfo ToProfileInfo(ProfileValue profileValue) { SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper(); try { var reuslt = new ProfileInfo(profileValue.LoginId, profileValue.IsAnonymous, profileValue.LastActivityDate.Value, profileValue.LastActivityDate.Value, 0); return(reuslt); } finally { sessionWrapper.Close(); } }
/// <summary> /// </summary> /// <param name="roleName"></param> /// <exception cref="ProviderException"></exception> public override void CreateRole(string roleName) { SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper(); try { var role = new Role(roleName); OrnamentContext.DaoFactory.MemberShipFactory.CreateRoleDao().Save(role); } catch (MemberShipException ex) { throw new ProviderException(ex.Message, ex); } finally { sessionWrapper.Close(); } }
/// <summary> /// </summary> /// <param name="users"></param> /// <returns></returns> private static string[] UserssToString(IList <User> users) { SessionWrapper wrapper = SessionManager.GetSessionWrapper(); try { //throw new NotImplementedException("UserssToString"); var s = new string[users.Count]; for (int i = 0; i < users.Count; i++) { s[i] = users[i].LoginId; } return(s); } finally { wrapper.Close(); } }
/// <summary> /// When overridden in a derived class, deletes profile properties and information for the supplied list of profiles. /// </summary> /// <returns> /// The number of profiles deleted from the data source. /// </returns> /// <param name="profiles"> /// A <see cref="T:System.Web.Profile.ProfileInfoCollection"></see> of information about profiles that are to be /// deleted. /// </param> public override int DeleteProfiles(ProfileInfoCollection profiles) { SessionWrapper wrapper = SessionManager.GetSessionWrapper(); try { var userName = new string[profiles.Count]; int i = 0; foreach (ProfileInfo info in profiles) { userName[i] = info.UserName; i++; } return(MemberShipFactory.CreateProfileDao().Delete(userName)); } finally { wrapper.Close(); } }
public void CloseSession(int index) { SessionWrapper session = null; // First remove from list lock (thisLock) { if (index > connectedSessions.Count - 1) { throw new IndexOutOfRangeException("Attempted to close session not found in list"); } session = connectedSessions[index]; // Session will remove self from list when its status changes } // Close session by disposing underlying WiFiDirectServiceSession session.Close(); RootPage.NotifyUser("Closed Session", NotifyType.StatusMessage); }
public override string[] GetAllRoles() { SessionWrapper s = SessionManager.GetSessionWrapper(); try { IList <Role> roles = OrnamentContext.DaoFactory.MemberShipFactory.CreateRoleDao().GetAll(); var result = new string[roles.Count]; for (int i = 0; i < result.Length; i++) { Role role = roles[i]; result[i] = role.Name; } return(result); } finally { s.Close(); } }
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { SessionWrapper s = SessionManager.GetSessionWrapper(); try { IUserDao userDao = OrnamentContext.DaoFactory.MemberShipFactory.CreateUserDao(); ReadOnlyCollection <Role> roles = OrnamentContext.DaoFactory.MemberShipFactory.CreateRoleDao().GetRolesByName(roleNames); IList <User> users = userDao.GetUsers(usernames); foreach (User u in users) { u.Roles.Clear(); userDao.Save(u); } } finally { s.Close(); } }
/// <summary> /// When overridden in a derived class, retrieves user profile data for all profiles in the data source. /// </summary> /// <returns> /// A <see cref="T:System.Web.Profile.ProfileInfoCollection"></see> containing user-profile information for all /// profiles in the data source. /// </returns> /// <param name="authenticationOption"> /// One of the <see cref="T:System.Web.Profile.ProfileAuthenticationOption"></see> values, specifying whether /// anonymous, authenticated, or both types of profiles are returned. /// </param> /// <param name="totalRecords">When this method returns, contains the total number of profiles.</param> /// <param name="pageIndex">The index of the page of results to return.</param> /// <param name="pageSize">The size of the page of results to return.</param> public override ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords) { SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper(); try { IList <ProfileValue> users; switch (authenticationOption) { case ProfileAuthenticationOption.Anonymous: users = MemberShipFactory.CreateProfileDao().GetAllAnonymous(pageIndex, pageSize, out totalRecords); break; case ProfileAuthenticationOption.Authenticated: users = MemberShipFactory.CreateProfileDao().GetAllAuthenticated(pageIndex, pageSize, out totalRecords); break; default: users = MemberShipFactory.CreateProfileDao().GetAll(pageIndex, pageSize, out totalRecords); break; } var result = new ProfileInfoCollection(); foreach (ProfileValue userProfile in users) { result.Add(ToProfileInfo(userProfile)); } return(result); } finally { sessionWrapper.Close(); } }
/// <summary> /// 获取属于某个角色且与指定的用户名相匹配的用户名的数组。 /// </summary> /// <param name="roleName">作为搜索范围的角色。</param> /// <param name="usernameToMatch">要搜索的用户名</param> /// <returns></returns> public override string[] FindUsersInRole(string roleName, string usernameToMatch) { SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper(); try { IList <User> uses = OrnamentContext.DaoFactory.MemberShipFactory.CreateUserDao().FindUsersInRole( roleName, usernameToMatch); var result = new string[uses.Count]; for (int i = 0; i < uses.Count; i++) { result[i] = uses[i].LoginId; } return(result); } finally { sessionWrapper.Close(); } }
private void Profile_MigrateAnonymous(object sender, ProfileMigrateEventArgs args) { SessionWrapper wrapper = SessionManager.GetSessionWrapper(); try { IUserProfileDao profileDao = OrnamentContext.DaoFactory.MemberShipFactory.CreateProfileDao(); ProfileValue anonymous = profileDao.FindByLoginId(args.AnonymousID); if (anonymous != null) { //合并anonymous profile ProfileBase currenProfile = HttpContext.Current.Profile; foreach (string key in anonymous.Properities.Keys) { currenProfile.SetPropertyValue(key, anonymous.Properities[key]); } profileDao.Delete(anonymous); currenProfile.Save(); AnonymousIdentificationModule.ClearAnonymousIdentifier(); } //最后,一更新Multi-lang的cookie,因此使用Profile的语言。 OrnamentContext.MemberShip.SwitchLanguage(OrnamentContext.MemberShip.CurrentUser().GetLanguage()); wrapper.Commit(); } catch (Exception ex) { ILog log = LogManager.GetLogger(typeof(GlobalContext)); log.Error(ex.Message, ex); } finally { wrapper.Close(); } }
/// <summary> /// Sets the values of the specified group of property settings. /// </summary> /// <param name="context"> /// A <see cref="T:System.Configuration.SettingsContext"></see> describing the current application usage. /// </param> /// <param name="collection"> /// A <see cref="T:System.Configuration.SettingsPropertyValueCollection"></see> representing the group of property /// settings to set. /// </param> /// <filterpriority>2</filterpriority> public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection) { SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper(); try { string userName = LoginId(context); IUserProfileDao profileDao = MemberShipFactory.CreateProfileDao(); ProfileValue profileValue = profileDao.FindByLoginId(userName) ?? new ProfileValue { LastActivityDate = DateTime.Now, IsAnonymous = !userIsAuthenticated(context), LoginId = userName }; foreach (SettingsPropertyValue settingsPropertyValue in collection) { if (profileValue.Properities.ContainsKey(settingsPropertyValue.Name)) { profileValue.Properities[settingsPropertyValue.Name] = settingsPropertyValue.PropertyValue; } else { profileValue.Properities.Add(settingsPropertyValue.Name, settingsPropertyValue.PropertyValue); } } profileDao.SaveOrUpdate(profileValue); sessionWrapper.Commit(); } finally { sessionWrapper.Close(); } }
/// <summary> /// When overridden in a derived class, retrieves profile information for profiles in which /// the last activity date occurred on or before the specified date and the user name matches the specified user name. /// </summary> /// <returns> /// A <see cref="T:System.Web.Profile.ProfileInfoCollection"></see> containing user profile information for inactive /// profiles where the user name matches the supplied usernameToMatch parameter. /// </returns> /// <param name="authenticationOption"> /// One of the <see cref="T:System.Web.Profile.ProfileAuthenticationOption"></see> values, specifying whether /// anonymous, authenticated, or both types of profiles are returned. /// </param> /// <param name="userInactiveSinceDate"> /// A <see cref="T:System.DateTime"></see> that identifies which user profiles are considered inactive. If the /// <see /// cref="P:System.Web.Profile.ProfileInfo.LastActivityDate"> /// </see> /// value of a user profile occurs on or before this date and time, the profile is considered inactive. /// </param> /// <param name="totalRecords">When this method returns, contains the total number of profiles.</param> /// <param name="pageIndex">The index of the page of results to return.</param> /// <param name="usernameToMatch">The user name to search for.</param> /// <param name="pageSize">The size of the page of results to return.</param> public override ProfileInfoCollection FindInactiveProfilesByUserName( ProfileAuthenticationOption authenticationOption, string usernameToMatch, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords) { SessionWrapper sessionWrapper = SessionManager.GetSessionWrapper(); try { var infos = new ProfileInfoCollection(); IQueryable <ProfileValue> profiles; switch (authenticationOption) { case ProfileAuthenticationOption.All: profiles = (from pf in MemberShipFactory.Profiles where pf.LastActivityDate < userInactiveSinceDate && pf.LoginId.StartsWith(usernameToMatch) select pf); totalRecords = (from pf in MemberShipFactory.Profiles where pf.LastActivityDate < userInactiveSinceDate && pf.LoginId.StartsWith(usernameToMatch) select pf).Count(); break; case ProfileAuthenticationOption.Anonymous: profiles = (from pf in MemberShipFactory.Profiles where pf.LastActivityDate < userInactiveSinceDate && pf.LoginId.StartsWith(usernameToMatch) && pf.IsAnonymous select pf); totalRecords = (from pf in MemberShipFactory.Profiles where pf.LastActivityDate < userInactiveSinceDate && pf.LoginId.StartsWith(usernameToMatch) && pf.IsAnonymous select pf).Count(); break; default: profiles = (from pf in MemberShipFactory.Profiles where pf.LastActivityDate < userInactiveSinceDate && pf.LoginId.StartsWith(usernameToMatch) && pf.IsAnonymous == false select pf); totalRecords = (from pf in MemberShipFactory.Profiles where pf.LastActivityDate < userInactiveSinceDate && pf.LoginId.StartsWith(usernameToMatch) && pf.IsAnonymous == false select pf).Count(); break; } foreach (ProfileValue prof in profiles) { User u = MemberShipFactory.CreateUserDao().GetByLoginId(prof.LoginId); infos.Add(new ProfileInfo(u.Name, prof.IsAnonymous, u.Other.LastActivityDate.Value, prof.LastActivityDate.Value, 30)); } sessionWrapper.Commit(); return(infos); } finally { sessionWrapper.Close(); } }