예제 #1
0
        static void Main(string[] args)
        {
            List<ServiceHost> hosts = new List<ServiceHost>();

            //Initialize Db. Requires ef packages for this project.
            try
            {
                Database.SetInitializer(new DatabaseInitializer<WcfTest3Entities>());
                using (var context = new WcfTest3Entities())
                {
                    context.Database.Initialize(force: true);
                    var x = context.Posts.Count();
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception while initializing db: " + ex);
            }

            try
            {
                var section = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection;
                if (section != null)
                {
                    Assembly implementationAssembly = Assembly.GetAssembly(typeof(CategoryService));

                    foreach (ServiceElement element in section.Services)
                    {
                        var serviceType = implementationAssembly.GetType(element.Name);
                        var host = new ServiceHost(serviceType);
                        host.Open();

                        Console.WriteLine(host.Description.Name + " opened.");
                        Console.WriteLine("Endpoints");
                        Console.WriteLine("=====================================");

                        foreach (var endpointAddr in host.Description.Endpoints)
                        {
                            Console.WriteLine(endpointAddr.Address);
                        }

                        Console.WriteLine(" ");
                    }
                }

                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }   
        }
        public override void Validate(string userName, string password)
        {
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                throw new FaultException("Username or password cannot be empty.");
            }

            using (var context = new WcfTest3Entities())
            {
                using (var userManager = new UserManager <IdentityUser>(new UserStore <IdentityUser>(context)))
                {
                    var user = userManager.Find(userName, password);
                    if (user == null)
                    {
                        throw new FaultException("Username or password is wrong.");
                    }
                }
            }
        }
예제 #3
0
        protected override bool CheckAccessCore(OperationContext operationContext)
        {
            using (var context = new WcfTest3Entities())
            {
                using (var userManager = new UserManager <IdentityUser>(new UserStore <IdentityUser>(context)))
                {
                    var identity = operationContext.ServiceSecurityContext.PrimaryIdentity;
                    var user     = userManager.FindByName(identity.Name);

                    if (user == null)
                    {
                        throw new FaultException("Username not known.");
                    }

                    var roles = userManager.GetRoles(user.Id).ToArray();

                    var principal = new GenericPrincipal(operationContext.ServiceSecurityContext.PrimaryIdentity, roles);
                    operationContext.ServiceSecurityContext.AuthorizationContext.Properties["Principal"] = principal;

                    return(true);
                }
            }
        }