Esempio n. 1
0
        /// <summary>
        /// Peforms guest user registration and generates a JWT.
        /// </summary>
        /// <returns>A JSON response containing a JWT.</returns>
        public async Task <IActionResult> OnPostAcquireTokenAsync()
        {
            if (ModelState.IsValid)
            {
                _logger.LogInformation("The user attempted to login");

                var serviceProvider        = HttpContext.RequestServices;
                var authenticationProvider = (AuthenticationProvider)
                                             serviceProvider.GetService(typeof(AuthenticationProvider));

                // TODO: This is fragile. Make stronger in Elderberry.
                var userNameEscaped     = InputName.Replace(" ", "+");
                var inputRoomLowercased = InputRoom.ToLowerInvariant();
                var email = $"{InputName.ToLowerInvariant().Replace(" ", "")}@{inputRoomLowercased}";

                var result = await authenticationProvider.Register(new UserDetails()
                {
                    Username = userNameEscaped,
                    Email    = email,
                });

                if (!result.Succeeded)
                {
                    ModelState.AddModelError(string.Empty, "A user by that name already exists in this room.");
                    return(Page());
                }

                var token = await authenticationProvider.Login(new LoginCredentials()
                {
                    Email = email,
                });

                if (token is null)
                {
                    ModelState.AddModelError(string.Empty, "An error occurred when trying to join the room.");
                    return(Page());
                }

                // Redirect back, with the token.
                ViewData["Token"] = token;
                return(Page());
            }

            // If we got this far, something failed, redisplay form
            _logger.LogError("The model state was not valid.");
            return(Page());
        }
Esempio n. 2
0
    private void InitInput()
    {
        if (!string.IsNullOrEmpty(InputName))
        {
            divInput.Controls.Add(new Label {
                ID = LabelPrefix + InputName.Replace(" ", string.Empty), Text = String.Format("<span>{0}:</span>", InputName)
            });

            TextBox tb = new TextBox {
                ID = TextBoxPrefix + InputName.Replace(" ", string.Empty)
            };

            if (!string.IsNullOrEmpty(InputMaxLength))
            {
                int tbMaxLength;
                if (Int32.TryParse(InputMaxLength, out tbMaxLength))
                {
                    tb.MaxLength = tbMaxLength;
                }
            }

            divInput.Controls.Add(tb);
        }
    }