示例#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);
        }
        public static StoreUserManager Create(
            IdentityFactoryOptions <StoreUserManager> options,
            IOwinContext context)
        {
            StoreIdentityDbContext dbContext = context.Get <StoreIdentityDbContext>();
            StoreUserManager       manager   =
                new StoreUserManager(new UserStore <StoreUser>(dbContext));

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

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

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