コード例 #1
0
        private static async Task <ConscienceAccount> CreateOrUpdateAccount(IUsersIdentityService identityService, ConscienceContext context, string name, string password, string role)
        {
            var account = context.Accounts.FirstOrDefault(a => a.UserName.ToLower() == name.ToLower());

            if (account == null)
            {
                account = new ConscienceAccount
                {
                    UserName     = name,
                    PasswordHash = "AA+JyvawjlityY0fyaRSr1qZkCgaIvU+bqTe6uShANUqcoG0EH2F6BcXq7hwnN7N1Q=="
                };
                var roleName = Enum.GetNames(typeof(RoleTypes)).First(r => r.ToLowerInvariant() == role.ToLowerInvariant());
                account.Roles.Add(new Role
                {
                    Name = roleName
                });
                context.Accounts.Add(account);
                context.SaveChanges();
            }

            account.PictureUrl = "/Content/images/uploaded/" + account.Id + ".jpg?_ts=101";
            context.SaveChanges();

            await identityService.ChangePasswordAsync(account.Id, password);

            return(account);
        }
コード例 #2
0
ファイル: AccountMutation.cs プロジェクト: nomada2/Conscience
        public AccountMutation(IUsersIdentityService accountService, AccountRepository accountRepo)
        {
            Name = "AccountMutation";

            Field <AccountGraphType>("addAccount",
                                     arguments: new QueryArguments(
                                         new QueryArgument <NonNullGraphType <StringGraphType> > {
                Name = "userName", Description = "user name"
            },
                                         new QueryArgument <StringGraphType> {
                Name = "email", Description = "email"
            },
                                         new QueryArgument <NonNullGraphType <StringGraphType> > {
                Name = "password", Description = "password"
            }
                                         ),
                                     resolve: context => accountService.RegisterAsync(context.GetArgument <string>("userName"), context.GetArgument <string>("email"), context.GetArgument <string>("password")))
            .AddPermission(RoleTypes.Admin).RequiresMembership();

            Field <AccountGraphType>("addRole",
                                     arguments: new QueryArguments(
                                         new QueryArgument <NonNullGraphType <IntGraphType> > {
                Name = "accountId", Description = "account id"
            },
                                         new QueryArgument <NonNullGraphType <RoleEnumeration> > {
                Name = "role", Description = "role"
            }
                                         ),
                                     resolve: context => accountRepo.AddRole(context.GetArgument <int>("accountId"), context.GetArgument <RoleTypes>("role")))
            .AddPermission(RoleTypes.Admin).RequiresMembership();

            Field <AccountGraphType>("changePassword",
                                     arguments: new QueryArguments(
                                         new QueryArgument <NonNullGraphType <IntGraphType> > {
                Name = "accountId", Description = "account id"
            },
                                         new QueryArgument <NonNullGraphType <StringGraphType> > {
                Name = "password", Description = "password"
            }
                                         ),
                                     resolve: context =>
            {
                accountService.ChangePasswordAsync(context.GetArgument <int>("accountId"), context.GetArgument <string>("password")).Wait();
                return(accountRepo.GetById(context.GetArgument <int>("accountId")));
            })
            .AddPermission(RoleTypes.Admin).RequiresMembership();

            Field <AccountGraphType>("login",
                                     arguments: new QueryArguments(
                                         new QueryArgument <NonNullGraphType <StringGraphType> > {
                Name = "userName", Description = "user name"
            },
                                         new QueryArgument <NonNullGraphType <StringGraphType> > {
                Name = "password", Description = "password"
            }
                                         ),
                                     resolve: context => accountService.LoginAsync(context.GetArgument <string>("userName"), context.GetArgument <string>("password"))).CurrentUserQuery();

            Field <AccountGraphType>("logout",
                                     resolve: context => { accountService.LogoffAsync(); return(null); }).RequiresMembership();
        }