コード例 #1
0
        private CJ.Models.LoginInputModel GetUserById(string id)
        {
            CJ.Models.LoginInputModel user = new CJ.Models.LoginInputModel();

            //user = UserManager.Get(id);
            return(user);
        }
コード例 #2
0
        public async Task <IActionResult> Login(LoginInputModel model, string button)
        {
            // 检查我们是否在授权请求的上下文中
            var context = await _interaction.GetAuthorizationContextAsync(model.ReturnUrl);

            // 用户单击“取消”按钮
            if (button != "login")
            {
                if (context != null)
                {
                    //如果用户取消,则将结果发送到身份服务器,就像
                    // 拒绝同意(即使该客户不需要同意)。
                    // 这将向客户端发回一个被拒绝的oidc错误响应。
                    await _interaction.GrantConsentAsync(context, ConsentResponse.Denied);

                    // 我们可以信任模特。因为GetAuthorizationContextAsync回来了
                    if (await _clientStore.IsPkceClientAsync(context.ClientId))
                    {
                        // 如果客户端是pkce,那么我们假设它是本地的,因此在如何
                        // 返回响应是为了最终用户获得更好的ux。
                        return(View("Redirect", new RedirectViewModel {
                            RedirectUrl = model.ReturnUrl
                        }));
                    }

                    return(Redirect(model.ReturnUrl));
                }
                else
                {
                    // 既然我们没有一个有效的上下文,那么我们就回到主页
                    return(Redirect("~/"));
                }
            }

            if (ModelState.IsValid)
            {
                //查找用户
                //var user = _userApp.GetUser(model.Username);
                var requestUri = "http://localhost:61927/api/Account";
                var response   = MyHttp.Post(requestUri, null, model);

                CJ.Models.LoginInputModel user = new CJ.Models.LoginInputModel();
                if (!string.IsNullOrEmpty(response.Data))
                {
                    user = JsonConvert.DeserializeObject <CJ.Models.LoginInputModel>(response.Data);
                }
                else
                {
                    user = null;
                };
                //对内存中的用户名/密码进行验证
                if (user != null && (model.Password == DESCrypt.Decrypt(user.Password)))
                {
                    await _events.RaiseAsync(new UserLoginSuccessEvent(user.Username, user.Id, user.Username));

                    // 如果用户选择“记住我”,只在此设置显式过期。
                    // 否则,我们将依赖于Cookie中间件中配置的过期。
                    AuthenticationProperties props = null;
                    if (AccountOptions.AllowRememberLogin && model.RememberLogin)
                    {
                        props = new AuthenticationProperties
                        {
                            IsPersistent = true,                                                             //认证信息是否跨域有效
                            ExpiresUtc   = DateTimeOffset.UtcNow.Add(AccountOptions.RememberMeLoginDuration) //凭据有效时间
                        };
                    }
                    ;
                    var serverUser = new IdentityServerUser(user.Id);
                    serverUser.DisplayName = user.DisplayName;
                    var claims = new[]
                    {
                        new Claim("Username", user.Username),
                        new Claim("DisplayName", user.DisplayName)
                    };
                    serverUser.AdditionalClaims = claims;
                    // issue authentication cookie with subject ID and username
                    await HttpContext.SignInAsync(serverUser);

                    if (context != null)
                    {
                        if (await _clientStore.IsPkceClientAsync(context.ClientId))
                        {
                            // 如果客户端是pkce,那么我们假设它是本地的,所以在如何
                            // 返回的响应是为最终用户提供更好的ux。
                            return(View("Redirect", new RedirectViewModel {
                                RedirectUrl = model.ReturnUrl
                            }));
                        }

                        // we can trust model.ReturnUrl since GetAuthorizationContextAsync returned non-null
                        return(Redirect(model.ReturnUrl));
                    }

                    //请求本地页
                    if (Url.IsLocalUrl(model.ReturnUrl))
                    {
                        return(Redirect(model.ReturnUrl));
                    }
                    else if (string.IsNullOrEmpty(model.ReturnUrl))
                    {
                        return(Redirect("~/"));
                    }
                    else
                    {
                        // 用户可能点击了恶意链接-应该被记录
                        throw new Exception("无效的返回URL");
                    }
                }

                await _events.RaiseAsync(new UserLoginFailureEvent(model.Username, "无效的凭据"));

                ModelState.AddModelError(string.Empty, AccountOptions.InvalidCredentialsErrorMessage);
            }

            // 出了问题,显示形式错误
            var vm = await BuildLoginViewModelAsync(model);

            return(View(vm));
        }