コード例 #1
0
        public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new BloggingSystemContext();
                using (context)
                {
                    this.ValidateUsername(model.Username);
                    this.ValidateNickname(model.DisplayName);
                    this.ValidateAuthCode(model.AuthCode);

                    var usernameLower = model.Username.ToLower();
                    var nicknameLower = model.DisplayName.ToLower();

                    var user = context.Users
                        .FirstOrDefault(usr => usr.Username == usernameLower ||
                            usr.DisplayName == nicknameLower);

                    if (user != null)
                    {
                        throw new InvalidOperationException("User exists");
                    }

                    user = new User()
                    {
                        Username = usernameLower,
                        DisplayName = model.DisplayName,
                        AuthCode = model.AuthCode,
                    };

                    context.Users.Add(user);
                    context.SaveChanges();

                    user.SessionKey = this.GenerateSessionKey(user.Id);
                    context.SaveChanges();

                    var loggedModel = new LoggedUserModel()
                    {
                        DisplayName = user.DisplayName,
                        SessionKey = user.SessionKey
                    };

                    var response = this.Request.CreateResponse(
                        HttpStatusCode.Created, loggedModel);

                    return response;
                }
            });

            return responseMsg;
        }
コード例 #2
0
        public HttpResponseMessage LoginUser(UserModel model)
        {
            var context = new BloggingSystemContext();
            var usernameLower = model.Username.ToLower();

            var user = context.Users.FirstOrDefault(
                usr => usr.Username == usernameLower ||
                       usr.AuthCode == model.AuthCode);

            try
            {
                if (user == null)
                {
                    throw new InvalidOperationException("Invalid username and/or password");
                }
            }
            catch (Exception e)
            {
                var errorResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message);
                return errorResponse;
            }

            if (user.SessionKey == null)
            {
                user.SessionKey = this.GenerateSessionKey(user.UserId);
                context.SaveChanges();
            }

            var loggedModel = new LoggedUserModel()
            {
                DisplayName = user.DisplayName,
                SessionKey = user.SessionKey
            };

            var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel);

            return response;
        }
コード例 #3
0
        public HttpResponseMessage PostLoginUser(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new BloggingSystemContext();
                using (context)
                {
                    this.ValidateUsername(model.Username);
                    this.ValidateAuthCode(model.AuthCode);

                    var usernameLower = model.Username.ToLower();
                    var nicknameLower = model.DisplayName.ToLower();

                    var user = context.Users
                        .FirstOrDefault(
                        usr => usr.Username == usernameLower &&
                            usr.AuthCode == model.AuthCode);

                    if (user == null)
                    {
                        throw new InvalidOperationException("Invalid name or password");
                    }

                    if (string.IsNullOrWhiteSpace(user.SessionKey))
                    {
                        var sesKey = this.GenerateSessionKey(user.Id);
                        ValidateSessionKey(sesKey);
                        user.SessionKey = sesKey;
                        context.SaveChanges();
                    }

                    var loggedModel = new LoggedUserModel()
                    {
                        DisplayName = user.DisplayName,
                        SessionKey = user.SessionKey
                    };

                    var response = this.Request.CreateResponse(
                        HttpStatusCode.OK, loggedModel);

                    return response;
                }
            });

            return responseMsg;
        }
コード例 #4
0
        public HttpResponseMessage RegisterUser(UserModel model)
        {
            var context = new BloggingSystemContext();
            var usernameLower = model.Username.ToLower();
            var displayNameLower = model.DisplayName.ToLower();

            try
            {
                this.ValidateUsername(model.Username);
                this.ValidateDisplayName(model.DisplayName);
                this.ValidateAuthCode(model.AuthCode);

                var existingUser = context.Users.FirstOrDefault(
                    usr => usr.Username == usernameLower ||
                           usr.DisplayName.ToLower() == displayNameLower);

                if (existingUser != null)
                {
                    throw new InvalidOperationException("Users exists");
                }
            }
            catch (Exception e)
            {
                var errorResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message);
                return errorResponse;
            }

            var user = new User()
            {
                Username = usernameLower,
                DisplayName = model.DisplayName,
                AuthCode = model.AuthCode
            };

            context.Users.Add(user);
            context.SaveChanges();

            user.SessionKey = this.GenerateSessionKey(user.UserId);
            context.SaveChanges();

            var loggedModel = new LoggedUserModel()
            {
                DisplayName = user.DisplayName,
                SessionKey = user.SessionKey
            };

            var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel);

            return response;
        }