当前位置: 首页>>代码示例>>C#>>正文


C# IErrorService.UserNameTaken方法代码示例

本文整理汇总了C#中IErrorService.UserNameTaken方法的典型用法代码示例。如果您正苦于以下问题:C# IErrorService.UserNameTaken方法的具体用法?C# IErrorService.UserNameTaken怎么用?C# IErrorService.UserNameTaken使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IErrorService的用法示例。


在下文中一共展示了IErrorService.UserNameTaken方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: UserModule

        public UserModule(IUserService userService, IErrorService error, IPasswordService passwordService, IUserCacheService userCacheService)
            : base("/user")
        {
            Post["/create"] = _ =>
            {
                if (Params.AreMissing("UserName", "Email")) return error.MissingParameters(Response);
                if (userService.GetUserByName(Params.UserName)!=null) return error.UserNameTaken(Response);
                if (!Params.Email.IsEmail()) return error.InvalidParameters(Response);
                var password = userService.CreateUser(Params.UserName, Params.Email);
                return (string.IsNullOrWhiteSpace(password)) ? error.InvalidParameters(Response) : Response.AsJson(new { Password = password });
            };

            Post["/validate"] = _ =>
            {
                if (Params.AreMissing("UserName", "Password")) return error.MissingParameters(Response);
                var user = userService.GetUserByName(Params.UserName);
                if (user == null || !passwordService.IsPasswordValid(user, Params.Password)) return error.NoUserForCredentials(Response);
                var key = userCacheService.Add(user);
                return (key == null) ? error.InvalidParameters(Response) : Response.AsJson(new { ThumbKey = key });
            };

            Post["/get"] = _ =>
            {
                if (Params.AreMissing("ThumbKey")) return error.MissingParameters(Response);
                if (!Params.ThumbKey.IsGuid()) return error.InvalidParameters(Response);
                var user = userCacheService.GetUser(Params.ThumbKey);
                return (user == null) ? error.NoUserForThumbkey(Response) : Response.AsJson(new { User = new { Id = user.Id, UserName = user.UserName, Email = user.Email } });
            };

            Post["/validate/thumbkey"] = _ =>
            {
                if (Params.AreMissing("ThumbKey")) return error.MissingParameters(Response);
                if (!Params.ThumbKey.IsGuid()) return error.InvalidParameters(Response);
                var isValid = userCacheService.Validate(Params.ThumbKey);
                return !isValid ? error.NoUserForThumbkey(Response) : HttpStatusCode.OK;
            };

            Post["/validate/name"] = _ =>
            {
                if (Params.AreMissing("UserName")) return error.MissingParameters(Response);
                var isValid = userService.GetUserByName(Params.UserName)==null;
                return !isValid ? error.UserNameTaken(Response) : HttpStatusCode.OK;
            };

            Post["/reset/password"] = _ =>
            {
                if (Params.AreMissing("UserName", "Password")) return error.MissingParameters(Response);
                var user = userService.GetUserByName(Params.UserName);
                if (user == null || !passwordService.IsPasswordValid(user, Params.Password)) return error.NoUserForCredentials(Response);
                var password = userService.ResetPassword(user);
                return (password == null) ? error.InvalidParameters(Response) : Response.AsJson(new { Password = password });
            };

            Post["/forgot-password/request"] = _ =>
            {
                if (Params.AreMissing("UserName", "Email")) return error.MissingParameters(Response);
                if (!Params.Email.IsEmail()) return error.InvalidParameters(Response);
                var user = userService.GetUserByName(Params.UserName);
                if (user == null) return error.NoUserForCredentials(Response);
                if (user.Email != Params.Email) return error.NoUserForEmail(Response);
                var token = userService.ForgotPasswordRequest(user);
                return (token == null) ? error.InvalidParameters(Response) : Response.AsJson(new { Token = token });
            };

            Post["/forgot-password/reset"] = _ =>
            {
                if (Params.AreMissing("UserName", "Token")) return error.MissingParameters(Response);
                if (!Params.Token.IsGuid()) return error.InvalidParameters(Response);
                var user = userService.GetUserByName(Params.UserName);
                if (user == null || !passwordService.IsForgotPasswordTokenValid(user, Params.Token)) return error.InvalidForgotPasswordToken(Response);
                var password = userService.ForgotPasswordReset(user);
                return (password == null) ? error.InvalidParameters(Response) : Response.AsJson(new { Password = password });
            };

            Post["/logout"] = _ =>
            {
                if (Params.AreMissing("ThumbKey")) return error.MissingParameters(Response);
                if (!Params.ThumbKey.IsGuid()) return error.InvalidParameters(Response);
                var success = userCacheService.Remove(Params.ThumbKey);
                return !success ? error.NoUserForThumbkey(Response) : HttpStatusCode.OK;
            };
        }
开发者ID:biofractal,项目名称:ThumbsUp,代码行数:82,代码来源:UserModule.cs


注:本文中的IErrorService.UserNameTaken方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。