Exemplo n.º 1
0
        public async Task <IHttpActionResult> Post(User newUser)
        {
            if (newUser != null)
            {
                SkylineUser user   = ToSkylineUser(newUser);
                var         result = await this._userManager.CreateAsync(user, newUser.Password);

                if (result.Succeeded)
                {
                    var dbUser = await this._userManager.FindByNameAsync(newUser.UserName);

                    if (dbUser != null)
                    {
                        var addRoleResult = await this._userManager.AddToRoleAsync(dbUser.Id, "User");

                        if (addRoleResult.Succeeded)
                        {
                            return(this.Ok(newUser));
                        }

                        AddErrorsToModelState(addRoleResult);
                    }
                }

                AddErrorsToModelState(result);
            }
            else
            {
                ModelState.AddModelError("", "User cannot be null");
            }

            return(this.BadRequest(ModelState));
        }
Exemplo n.º 2
0
        public async Task Post()
        {
            IAuthenticationManager authMgr = HttpContext.Current.GetOwinContext().Authentication;
            SkylineUserManager     userMrg = HttpContext.Current.GetOwinContext().GetUserManager <SkylineUserManager>();
            SkylineUser            user    = await userMrg.FindAsync("Admin", "secret");

            authMgr.SignIn(await userMrg.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie));
            // return RedirectToAction("Index");
        }
Exemplo n.º 3
0
 private User ToUser(SkylineUser user)
 {
     return(new User
     {
         UserName = user.UserName,
         Email = user.Email,
         DOB = user.DOB,
         FirstName = user.FirstName,
         LastName = user.LastName,
         MiddleName = user.MiddleName
     });
 }
Exemplo n.º 4
0
        //private SkylineUserManager _userManager;

        //private IComparable c;
        //public SkylineAuthProvider(SkylineUserManager manager)
        //{
        //    //this.c = cc;
        //    _userManager = manager;
        //}

        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            SkylineUserManager storeUserMgr = (SkylineUserManager)GlobalConfiguration.Configuration.DependencyResolver.BeginScope().GetService(typeof(SkylineUserManager));
            SkylineUser        user         = await storeUserMgr.FindAsync(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "The username or password is incorrect");
            }
            else
            {
                //_userManager.CreateIdentity()
                ClaimsIdentity ident = await storeUserMgr.CreateIdentityAsync(user, "Custom");

                AuthenticationTicket ticket = new AuthenticationTicket(ident, new AuthenticationProperties());
                context.Validated(ticket);
                context.Request.Context.Authentication.SignIn(ident);
            }
        }
Exemplo n.º 5
0
        protected override void Seed(SkylineDbContext context)
        {
            SkylineUserManager userMgr  = new SkylineUserManager(new UserStore <SkylineUser>(context));
            SkylineRoleManager roleMgr  = new SkylineRoleManager(new RoleStore <SkylineRole>(context));
            string             userName = "******";
            string             password = "******";
            string             email    = "*****@*****.**";

            string[] roles = { "Administrator", "User", "Editor" };
            foreach (var role in roles)
            {
                if (!roleMgr.RoleExists(role))
                {
                    roleMgr.Create(new SkylineRole(role));
                }
            }

            SkylineUser user = userMgr.FindByName(userName);

            if (user == null)
            {
                // use default validator
                userMgr.UserValidator     = new UserValidator <SkylineUser>(userMgr);
                userMgr.PasswordValidator = new PasswordValidator();
                userMgr.Create(new SkylineUser
                {
                    UserName  = userName,
                    Email     = email,
                    FirstName = "Admin",
                    LastName  = "Yu",
                    DOB       = null
                }, password);
                user = userMgr.FindByName(userName);
            }
            if (!userMgr.IsInRole(user.Id, "Administrator"))
            {
                userMgr.AddToRole(user.Id, "Administrator");
            }

            GetNews().ForEach(news => context.News.Add(news));
            context.SaveChanges();
        }