Exemplo n.º 1
0
        protected override void Seed(FlightIdentityDbContext context)
        {
            FlightUserManager userMgr = new FlightUserManager(new UserStore <FlightUser>(context));
            FlightRoleManager roleMgr = new FlightRoleManager(new RoleStore <FlightRole>(context));

            string roleName = "Adminstrators";
            string userName = "******";
            string password = "******";
            string email    = "*****@*****.**";

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

            FlightUser user = userMgr.FindByName(userName);

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

            if (!userMgr.IsInRole(user.Id, roleName))
            {
                userMgr.AddToRole(user.Id, roleName);
            }
            base.Seed(context);
        }
Exemplo n.º 2
0
        public static FlightUserManager Create(IdentityFactoryOptions <FlightUserManager> options, IOwinContext context)
        {
            FlightIdentityDbContext dbContext = context.Get <FlightIdentityDbContext>();
            FlightUserManager       manager   = new FlightUserManager(new UserStore <FlightUser>(dbContext));

            return(manager);
        }
        public override async Task GrantResourceOwnerCredentials(
            OAuthGrantResourceOwnerCredentialsContext context)
        {
            FlightUserManager flightUserMgr =
                context.OwinContext.Get <FlightUserManager>("AspNet.Identity.Owin:" + typeof(FlightUserManager).AssemblyQualifiedName);
            FlightUser user = await flightUserMgr.FindAsync(context.UserName, context.Password);

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

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