Пример #1
0
        public async Task <IActionResult> SignIn([FromForm] OAuthSignInModel model)
        {
            Guid sagaId;
            var  context = _dataProtector.Unprotect(model.ProtectedOAuthContext);

            if (!Guid.TryParse(context, out sagaId))
            {
                return(BadRequest());
            }
            var command = new PasswordAuthenticateCommand
            {
                UserName = model.UserName,
                Password = model.Password
            };
            var authResult = await _sagaBus.InvokeAsync <PasswordAuthenticateCommand, AuthenticationResult>(command);

            if (!authResult.IsCredentialVaild)
            {
                ModelState.AddModelError(nameof(OAuthSignInModel.UserName), "Invaild user name");
                ModelState.AddModelError(nameof(OAuthSignInModel.Password), "Or invaild password");
                return(View("SignIn"));
            }
            await _signinService.CookieSignInAsync(AuthenticationSchemes.OAuth20Cookie, authResult.User, model.RememberMe);

            var message = new OAuth20SignInMessage(sagaId)
            {
                UserName = model.UserName,
            };
            var oauth20Result = await _sagaBus.SendAsync <OAuth20SignInMessage, OAuth20Result>(message);

            switch (oauth20Result.State)
            {
            case OAuth20State.RequirePermissionGrant:
                return(PermissionGrantView(oauth20Result));

            case OAuth20State.AuthorizationCodeGenerated:
                return(AuthorizationCodeRedirect(oauth20Result));

            case OAuth20State.Finished:
                return(ImplictRedirect(oauth20Result));

            default:
                return(BadRequest());
            }
        }
Пример #2
0
        public async Task <IActionResult> SignIn([FromForm] SignInViewModel model)
        {
            var command = new PasswordAuthenticateCommand
            {
                UserName = model.UserName,
                Password = model.Password
            };
            var authResult = await _sagaBus.InvokeAsync <PasswordAuthenticateCommand, AuthenticationResult>(command);

            if (!authResult.IsCredentialVaild)
            {
                ModelState.AddModelError(nameof(SignInViewModel.UserName), "Invaild user name");
                ModelState.AddModelError(nameof(SignInViewModel.Password), "Or invaild password");
                return(View("SignIn"));
            }
            await _signinService.CookieSignInAsync(AuthenticationSchemes.PortalCookie, authResult.User, model.RememberMe);

            return(Redirect(model.ReturnUrl));
        }