コード例 #1
0
        [ValidateAntiForgeryToken] // Prevents XSRF/CSRF attacks
        public async Task <IActionResult> Index(PostAuthenticateRequestModel postAuthenticateRequestModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    // Send an API request to authenticate the new user
                    PostAuthenticateResponseModel postAuthenticateResponseModel = await _moviemindAPIService.Authenticate(postAuthenticateRequestModel);

                    _stateManagementService.SetState(postAuthenticateResponseModel);

                    // Redirect to the home page
                    return(RedirectToRoute(new { action = "Index", controller = "Home" }));
                }
                catch (MovieMindException e)
                {
                    TempData["ApiError"] = e.Message;
                }
            }

            return(View(postAuthenticateRequestModel));
        }
コード例 #2
0
        [ValidateAntiForgeryToken] // Prevents XSRF/CSRF attacks
        public async Task <IActionResult> Index(PostUserModel postUserModel, string rememberMe)
        {
            if (postUserModel.Password != postUserModel.ConfirmPassword)
            {
                ModelState.AddModelError("ConfirmPassword", _localizer["Passwords are not the same"]);
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (postUserModel.Roles == null)
                    {
                        postUserModel.Roles = new List <String>
                        {
                            "Guest"
                        };
                    }
                    else if (postUserModel.Roles.Count == 0)
                    {
                        postUserModel.Roles.Add("Guest");
                    }
                    // Send an API request to create the new user
                    GetUserModel getUserModel = await _moviemindAPIService.PostModel <PostUserModel, GetUserModel>(postUserModel, "users");

                    // When the user was successfully created send an API request to authenticate the new user
                    PostAuthenticateRequestModel postAuthenticateRequestModel = new PostAuthenticateRequestModel
                    {
                        UserName = postUserModel.UserName,
                        Password = postUserModel.Password,
                    };

                    PostAuthenticateResponseModel postAuthenticateResponseModel = await _moviemindAPIService.Authenticate(postAuthenticateRequestModel);

                    _stateManagementService.SetState(postAuthenticateResponseModel);

                    // Redirect to the home page
                    return(RedirectToRoute(new { action = "Index", controller = "Home" }));
                }
                catch (MovieMindException e)
                {
                    TempData["ApiError"] = e.Message;
                }
            }

            return(View(postUserModel));
        }