Example #1
0
        public async Task <IActionResult> Index(LoginCommand command)
        {
            #region если Default DataSource задан в настройках
            if (!string.IsNullOrEmpty(_connectionOptions.DataSource))
            {
                command.DataSource = _connectionOptions.DataSource;

                ModelStateEntry mse;
                ModelState.TryGetValue("DataSource", out mse);
                if (mse != null)
                {
                    mse.Errors.Clear();
                    mse.ValidationState = ModelValidationState.Valid;
                }
            }
            #endregion

            LoginCommandResult result = new LoginCommandResult();

            if (ModelState.IsValid)
            {
                result = _commandDispatcher.Dispatch <LoginCommand, LoginCommandResult>(command);

                // Authenticate
                if (result.Status != LoginStatus.Failure)
                {
                    Claim[] claims =
                    {
                        new Claim(JwtClaimTypes.Name,               command.UserName),
                        new Claim(ConnectionClaimTypes.Password,    command.Password),
                        new Claim(ConnectionClaimTypes.DataSource,  command.DataSource),
                        new Claim(JwtClaimTypes.Subject,            command.DataSource + command.UserName + command.Password),
                        new Claim(JwtClaimTypes.IdentityProvider,   "idsvr"),
                        new Claim(JwtClaimTypes.AuthenticationTime, DateTime.UtcNow.ToEpochTime().ToString())
                    };

                    ClaimsIdentity ci = new ClaimsIdentity(claims, "password", JwtClaimTypes.Name, JwtClaimTypes.Role);

                    // persistent cookie
                    AuthenticationProperties props = new AuthenticationProperties
                    {
                        IsPersistent = _appSettings.Persistent,
                        ExpiresUtc   = DateTimeOffset.UtcNow.AddSeconds(_appSettings.Timeout)
                    };

                    await HttpContext.Authentication.SignInAsync(IdentityServerConstants.DefaultCookieAuthenticationScheme, new ClaimsPrincipal(ci), props);
                }
            }
            else
            {
                result.Message = string.Join("; ",
                                             ModelState.Values
                                             .SelectMany(x => x.Errors)
                                             .Select(x => x.ErrorMessage)
                                             );
            }

            return(new JsonResultIe(result));
        }
Example #2
0
        public async Task <ActionResult <LoginCommandResult> > Login([FromBody] LoginCommand command)
        {
            LoginCommandResult loginResponse = await Mediator.Send(command);

            if (loginResponse == null)
            {
                return(this.Unauthorized());
            }

            return(Ok(loginResponse));
        }
Example #3
0
    private void OnLoggedIn(LoginResult result)
    {
        Debug.Log("entity rtokem " + result.EntityToken.Entity.Type);
        LoginCommandResult loginCommandResult = new LoginCommandResult
        {
            PlayFabId  = result.PlayFabId,
            EntityId   = result.EntityToken.Entity.Id,
            EntityType = result.EntityToken.Entity.Type
        };

        Callback?.Invoke(loginCommandResult);
    }
Example #4
0
        public async Task <IActionResult> Index(ChangePasswordCommand command)
        {
            LoginResult result = new LoginResult()
            {
                Status = LoginStatus.Failure
            };

            if (ModelState.IsValid)
            {
                LoginCommandResult commandResult = _commandDispatcher.Dispatch <ChangePasswordCommand, LoginCommandResult>(command);
                result.Status  = commandResult.Status;
                result.Message = commandResult.Message;

                if (result.Status != LoginStatus.Failure)
                {
                    result.ReturnUrl = command.ReturnUrl ?? "~/";
                    await HttpContext.Authentication.SignOutAsync(IdentityServerConstants.DefaultCookieAuthenticationScheme);

                    ClaimsIdentity ci = HttpContext.User.Identity as ClaimsIdentity;
                    if (ci != null)
                    {
                        Claim claimPsw = ci.FindFirst(ConnectionClaimTypes.Password);

                        ci.TryRemoveClaim(claimPsw);
                        ci.AddClaim(new Claim(ConnectionClaimTypes.Password, command.NewPassword));

                        // persistent cookie
                        AuthenticationProperties props = new AuthenticationProperties
                        {
                            IsPersistent = _appSettings.Persistent,
                            ExpiresUtc   = DateTimeOffset.UtcNow.AddSeconds(_appSettings.Timeout)
                        };

                        await HttpContext.Authentication.SignInAsync(IdentityServerConstants.DefaultCookieAuthenticationScheme, new ClaimsPrincipal(ci), props);
                    }
                }
            }
            else
            {
                result.Message = string.Join("; ",
                                             ModelState.Values
                                             .SelectMany(x => x.Errors)
                                             .Select(x => x.ErrorMessage)
                                             );
            }

            return(new JsonResultIe(result));
        }
Example #5
0
        public async Task <IActionResult> LoginAsync([FromServices] IMediator medator)
        {
            var command = new LoginCommand("User", "User");
            LoginCommandResult commandResult = await medator.Send(command);

            var result = commandResult.Result.Match <ActionResult>(
                success =>
            {
                return(Json(new { token = "success" }));
            },
                notFound =>
            {
                return(NotFound());
            });

            return(result);
        }