Esempio n. 1
0
        public HttpResponseMessage Register(Register Register)
        {
            // ensure users can register
            var registrationSettings = _orchardServices.WorkContext.CurrentSite.As <RegistrationSettingsPart>();

            if (!registrationSettings.UsersCanRegister)
            {
                return(Request.CreateResponse(HttpStatusCode.MethodNotAllowed, new uError("Method Not Allowed", 405)));
            }

            if (Register.Password.Length < MinPasswordLength)
            {
                return(Request.CreateResponse(HttpStatusCode.MethodNotAllowed, new uError("Method Not Allowed", 405)));
            }

            if (!_profileService.VerifyUserUnicity(Register.Email, Register.Email))
            {
                return(Request.CreateResponse(HttpStatusCode.Conflict, new uError("Conflict on the Server", 409)));
            }
            ApplicationRecord apprecord = _applicationsService.GetApplicationByKey(Register.ApiKey);

            if (apprecord == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound, new uError("Not Found", 404)));
            }

            if (ValidateRegistration(Register))
            {
                // Attempt to register the user
                // No need to report this to IUserEventHandler because _membershipService does that for us
                var user = _membershipService.CreateUser(new CreateUserParams(Register.Email, Register.Password, Register.Email, null, null, false));

                if (user != null)
                {
                    UserProfilePart profile = user.As <UserProfilePart>();
                    if (profile != null)
                    {
                        profile.FirstName = Register.FirstName;
                        profile.LastName  = Register.LastName;
                    }
                    if (user.As <UserPart>().EmailStatus == UserStatus.Pending)
                    {
                        var siteUrl = _orchardServices.WorkContext.CurrentSite.BaseUrl;
                        //if (String.IsNullOrWhiteSpace(siteUrl))
                        //{
                        //    siteUrl = Request.ToRootUrlString();
                        //}
                        //var url = Url.Route("challengeemail", new { controller = "login", action = "ChallengeEmail", returnUrl = "hello" });

                        var _Url = new System.Web.Mvc.UrlHelper(System.Web.HttpContext.Current.Request.RequestContext);

                        _profileService.SendChallengeMail(
                            apprecord,
                            user.As <UserPart>(),
                            nonce =>

                            _Url.MakeAbsolute(
                                _Url.Action("ChallengeEmail", "Account", new
                        {
                            Area  = "Contrib.Foundation.UserProfile",
                            nonce = nonce
                        }
                                            )
                                )

                            //_Url.MakeAbsolute(
                            //    _Url.Action("ChallengeEmail", "login", new
                            //        {
                            //            httproute = true,
                            //            area = "Contrib.Foundation.UserProfile",
                            //            nonce = nonce
                            //        }
                            //    )
                            //)

                            //protocolChallengeEmail(nonce)
                            );
                        _userEventHandler.SentChallengeEmail(user);
                        return(Request.CreateResponse(HttpStatusCode.Created, new uError("Create", 201, false)));
                    }

                    if (user.As <UserPart>().RegistrationStatus == UserStatus.Pending)
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotModified, new uError("Not Modified", 304)));
                    }

                    _authenticationService.SignIn(user, false);
                    return(Request.CreateResponse(HttpStatusCode.OK, new uError("OK", 200)));
                }

                return(Request.CreateResponse(HttpStatusCode.InternalServerError, new uError("Internal Server Error", 500)));
            }

            return(Request.CreateResponse(HttpStatusCode.InternalServerError, new uError("Internal Server Error", 500)));;
        }