Ejemplo n.º 1
0
        protected override void Seed(StoreIdentityDbContext context)
        {
            StoreUserManager userMgr = new StoreUserManager(new UserStore <StoreUser>(context));

            StoreRoleManager roleMgr = new StoreRoleManager(new RoleStore <StoreRole>(context));

            string roleName = "Administrators";
            string userName = "******";
            string passWord = "******";
            string email    = "*****@*****.**";

            if (!roleMgr.RoleExists(roleName))
            {
                roleMgr.Create(new StoreRole(roleName));
            }

            StoreUser user = userMgr.FindByName(userName);

            if (user == null)
            {
                userMgr.Create(new StoreUser {
                    UserName = userName, Email = email
                }, passWord);
                user = userMgr.FindByName(userName);
            }

            if (!userMgr.IsInRole(user.Id, roleName))
            {
                userMgr.AddToRole(user.Id, roleName);
            }

            base.Seed(context);
        }
Ejemplo n.º 2
0
        public static StoreUserManager Create(IdentityFactoryOptions <StoreUserManager> options, IOwinContext context)
        {
            StoreIdentityDbContext dbContext = context.Get <StoreIdentityDbContext>();
            StoreUserManager       manager   = new StoreUserManager(new UserStore <StoreUser>(dbContext));

            return(manager);
        }
Ejemplo n.º 3
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            StoreUserManager storeUserManager = context.OwinContext.Get <StoreUserManager>("AspNet.Identity.Owin:" + typeof(StoreUserManager).AssemblyQualifiedName);
            StoreUser        user             = await storeUserManager.FindAsync(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "The username or password is incorrect");
            }
            else
            {
                ClaimsIdentity identity = await storeUserManager.CreateIdentityAsync(user, "Custom");

                AuthenticationTicket ticket = new AuthenticationTicket(identity, new AuthenticationProperties());
                context.Validated(ticket);
                context.Request.Context.Authentication.SignIn(identity);
            }
        }