Beispiel #1
0
        public void GetService_GetsService()
        {
            Object actualInstance = resolver.GetService(typeof(AContext));
            Type   expectedType   = typeof(AContext);

            Assert.IsInstanceOf(expectedType, actualInstance);
        }
Beispiel #2
0
        public void GivenSingletonConfiguration_WhenWeResolveDependencies_ShouldTheSameInstance()
        {
            //arrange
            var depResolver = new NinjectResolver(new FakeNinjectModule());
            //act
            var instanceFirstCall  = depResolver.GetService(typeof(IFakeService));
            var instanceSecondCall = depResolver.GetService(typeof(IFakeService));

            //assert
            Assert.That(instanceFirstCall, Is.EqualTo(instanceSecondCall));
        }
Beispiel #3
0
        public void GivenNoConfiguration_ShouldReturnsNull()
        {
            var depResolver = new NinjectResolver();

            //act & assert
            Assert.That(depResolver.GetService(typeof(IFakeService)), Is.Null);
        }
Beispiel #4
0
        public void GivenSingletonConfiguration_WhenResolveDepedency_ShouldReturnsTheRightType()
        {
            //arrange
            var depResolver = new NinjectResolver(new FakeNinjectModule());

            //act & assert
            Assert.That(depResolver.GetService(typeof(IFakeService)), Is.TypeOf <FakeService>());
        }
        public void NinjectResolver_DoesNot_Resolve_NonRegistered_ContactRepository_Test()
        {
            var kernel = new StandardKernel();

            var resolver = new NinjectResolver(kernel);
            var instance = resolver.GetService(typeof(IContactRepository));

            Assert.IsNull(instance);
        }
        public void NinjectResolver_DoesNot_Resolve_NonRegistered_ContactRepository_Test()
        {
            var kernel = new StandardKernel();

            var resolver = new NinjectResolver(kernel);
            var instance = resolver.GetService(typeof(IContactRepository));

            Assert.IsNull(instance);
        }
        public void NinjectResolver_Resolves_Registered_ContactRepository_Test()
        {
            var kernel = new StandardKernel();
            kernel.Bind<IContactRepository>().ToConstant(new InMemoryContactRepository());

            var resolver = new NinjectResolver(kernel);
            var instance = resolver.GetService(typeof(IContactRepository));

            Assert.IsNotNull(instance);
        }
        public void NinjectResolver_Resolves_Registered_ContactRepository_Test()
        {
            var kernel = new StandardKernel();

            kernel.Bind <IContactRepository>().ToConstant(new InMemoryContactRepository());

            var resolver = new NinjectResolver(kernel);
            var instance = resolver.GetService(typeof(IContactRepository));

            Assert.IsNotNull(instance);
        }
Beispiel #9
0
        protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
        {
            if (!FormsAuthentication.CookiesSupported)
            {
                return;
            }

            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                return;
            }

            try
            {
                var login       = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value)?.Name;
                var userService = (IUserService)_ninjectResolver.GetService(typeof(IUserService));
                var user        = userService.GetUser(login);
                if (user == null)
                {
                    return;
                }

                var roles = user.Roles;
                var name  = user.FirstName + " " + user.LastName;

                if (roles != null)
                {
                    var claims = new List <Claim>
                    {
                        new Claim(ClaimTypes.Name, name, ClaimValueTypes.String, null),
                        new Claim(ClaimTypes.Email, login, ClaimValueTypes.String, null),
                    };
                    foreach (var role in roles)
                    {
                        claims.Add(new Claim(ClaimTypes.Role, role.Name));
                    }

                    HttpContext.Current.User = new ClaimsPrincipal(new ClaimsIdentity(claims, "ApplicationCookie", ClaimTypes.Name, ClaimTypes.Role));
                }
            }
            catch (Exception)
            {
                //somehting went wrong
            }
        }
Beispiel #10
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var createservice = new NinjectResolver(Startup.CreateKernel());
            var service       = createservice.GetService <IUserService>();
            var password      = Utilities.Encrypt(context.Password);
            var User          = service.GetUserbyCredential(context.UserName, password).Result;

            if (User == null)
            {
                context.SetError("Error", "Provided credentials are not valid");
                context.Rejected();
                return;
            }
            else
            {
                ClaimsIdentity identity = new ClaimsIdentity(context.Options.AuthenticationType);
                identity = new ClaimsIdentity(Startup.OAuthServerOptions.AuthenticationType);
                identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));

                var props = new AuthenticationProperties(new Dictionary <string, string>
                {
                    {
                        "Id", User.Id.ToString()
                    },
                    {
                        "userTypeId", User.UserTypeId.ToString()
                    },
                    {
                        "fullName", User.FullName.ToString()
                    },
                    {
                        "email", User.Email.ToString()
                    },
                });

                var ticket = new AuthenticationTicket(identity, props);

                context.Validated(ticket);
            }
        }