public AspnetMembershipController(IAspnetMembershipService aspnetMembershipService, IAspnetUsersService aspnetUsersService, IMapper mapper) { _aspnetMembershipService = aspnetMembershipService; _aspnetUsersService = aspnetUsersService; _mapper = mapper; }
public TokenController(IConfiguration configuration, IAspnetMembershipService aspnetMembershipService) { _configuration = configuration; _AspnetMembershipService = aspnetMembershipService; }
/// <summary> /// Initializes the provider. /// </summary> /// <param name="config">A collection of the name/value pairs representing the provider-specific /// attributes specified in the configuration for this provider.</param> /// <param name="name">The friendly name of the provider.</param> /// <exception cref="ArgumentNullException">The name of the provider is null.</exception> /// <exception cref="InvalidOperationException">An attempt is made to call <see cref="Initialize(System.String,System.Collections.Specialized.NameValueCollection)"></see> on a provider after the provider has already been initialized.</exception> /// <exception cref="ArgumentException">The name of the provider has a length of zero.</exception> public override void Initialize(string name, NameValueCollection config) { // Initialize values from Web.config. if (null == config) { throw (new ArgumentNullException("config")); } if (string.IsNullOrEmpty(name)) { name = "NHibernateRoleProvider"; } if (string.IsNullOrEmpty(config["description"])) { config.Remove("description"); config.Add("description", "NHibernate Role Provider"); } // Call the base class implementation. base.Initialize(name, config); if (_aspnetMembershipService == null) { IApplicationContext context = ContextRegistry.GetContext(); _aspnetMembershipService = (IAspnetMembershipService)context.GetObject("IAspnetMembershipService"); } if (_aspnetApplicationService == null) { IApplicationContext context = ContextRegistry.GetContext(); _aspnetApplicationService = (IAspnetApplicationService)context.GetObject("IAspnetApplicationService"); } if (_aspnetRoleService == null) { IApplicationContext context = ContextRegistry.GetContext(); _aspnetRoleService = (IAspnetRoleService)context.GetObject("IAspnetRoleService"); } string appName = ConfigurationUtil.GetConfigValue(config["applicationName"], System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath); application = _aspnetApplicationService.CreateOrLoadApplication(appName); if (config["writeExceptionsToEventLog"] != null) { if (config["writeExceptionsToEventLog"].ToUpper() == "TRUE") { pWriteExceptionsToEventLog = true; } } // Load configuration data. //application = _applicationService.CreateOrLoadApplication(appName); //application = // NHibernateProviderEntityHelper.CreateOrLoadApplication( // ConfigurationUtil.GetConfigValue(config["applicationName"], HostingEnvironment.ApplicationVirtualPath)); }
/// <summary> /// gets a value indicating whether the specified user is in the specified role for the configured application name. /// </summary> /// <param name="username">the name of the user to search for.</param> /// <param name="rolename">the name of the role to search in.</param> /// <returns> /// <c>true</c> if the specified user is in the specified role for the configured application name; /// otherwise, <c>false</c>. /// </returns> public override bool IsUserInRole(string username, string rolename) { // assume the given role is not associated to the given user. bool isinrole = false; //// check against the data store if the role has been assigned to the given user. try { if (_aspnetMembershipService == null) { IApplicationContext context = ContextRegistry.GetContext(); _aspnetMembershipService = (IAspnetMembershipService)context.GetObject("IAspnetMembershipService"); } if (_aspnetRoleService == null) { IApplicationContext context = ContextRegistry.GetContext(); _aspnetRoleService = (IAspnetRoleService)context.GetObject("IAspnetRoleshipService"); } AspnetMembership user = _aspnetMembershipService.GetMembership(username); AspnetRole role = _aspnetRoleService.GetAspnetRole(rolename); if ((null != user) && (null != role)) { //TODO wint linq :int appuserrole = _aspnetRoleService.IsUserInrole(application.ApplicationId, user.UserId, role.RoleId); int appuserrole = user.AspnetRoles.Count(n => n.RoleId.Equals(role.RoleId)); isinrole = (0 < appuserrole); } } catch (Exception ex) { if (WriteExceptionsToEventLog) { WriteToEventLog(ex, "IsUserInRole"); } else { throw ExceptionUtil.NewProviderException(this, Resources.Role_UnableToFindUserInRole, ex); } } // return the result of the operation. return isinrole; }
/// <summary> /// Gets a list of the roles that a specified user is in for the configured application name. /// </summary> /// <param name="username">the name of the user for whom to return a list of roles.</param> /// <returns> /// A string array containing the names of all the roles that the specified user is in for /// the configured application name. /// </returns> public override string[] GetRolesForUser(string username) { // Prepare a placeholder for the roles. string[] roleNames = new string[0]; // Load the list from the data store. try { if (_aspnetMembershipService == null) { IApplicationContext context = ContextRegistry.GetContext(); _aspnetMembershipService = (IAspnetMembershipService)context.GetObject("IAspnetMembershipService"); } AspnetMembership user = _aspnetMembershipService.GetMembership(username); if (user != null) { IList<AspnetRole> userRoles = user.AspnetRoles; if ((user.AspnetRoles != null) | (user.AspnetRoles.Count > 0)) { IList<string> roles = (from n in user.AspnetRoles select n.RoleName).ToList<string>(); //TODO with Linq = _aspnetUsersInRoleService.GetRolesNamesForUser(application.ApplicationId, user.UserId); if (null != roles) { roleNames = new string[roles.Count]; for (int i = 0; i < roles.Count; i++) { roleNames[i] = roles[i].ToString(); } } } } } catch (Exception ex) { if (WriteExceptionsToEventLog) { WriteToEventLog(ex, "GetRolesForUser"); } else { throw ExceptionUtil.NewProviderException(this, Resources.Role_UnableToGetRolesForUser, ex); } } // Return the result of the operation. return roleNames; }