Esempio n. 1
0
        public HttpResponseMessage Post(LoginRequest request)
        {
            LoginResponse response = new LoginResponse();
            if (request == null)
            {
                response.IsSuccess = false;
                response.Result = "请求的数据不能为空";

                return BuildErrorResult(HttpStatusCode.BadRequest, response);
            }
            if (String.IsNullOrWhiteSpace(request.Username))
            {
                response.IsSuccess = false;
                response.Result = "用户名不能为空";
                return BuildErrorResult(HttpStatusCode.BadRequest, response);
            }


            var username = request.Username;


            if (Dependency.Resolve<IAuthenticationService>().Validate(ref username, request.Password))
            {
                CheckTwoFactorAuthentication(username, request);

                WebSecurityHelper.SetAuthenticationTicket(username, false);
                response.IsSuccess = true;
                response.Result = "登录成功";
                //var user = (UserDefinition) Authorization.UserDefinition;
                if (Dependency.Resolve<IUserRetrieveService>().ByUsername(username) is UserDefinition user)
                {
                    response.TenantId = user.TenantId;
                    string sql = $"select * from [dbo].[UserRoles] where UserId = {user.UserId}";
                    int roleId = 0;
                    using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Default"].ConnectionString))
                    {
                        var userRole = conn.Query<UserRoleRow>(sql).FirstOrDefault();
                        if (userRole != null && userRole.RoleId != null) roleId = userRole.RoleId.Value;
                    }
                   
                    //var userRole = new UserRoleRepository().List(SqlConnections.NewFor<UserRoleRow>(), new Administration.UserRoleListRequest{UserID = user.UserId}).Entities.First();
                    if (username == "admin" || roleId == 1)
                    {
                        response.IsAdmin = true;
                    }
                }


                return BuildSuccessResult(HttpStatusCode.OK, response);
            }

            response.IsSuccess = false;
            response.Result = "用户名或密码不正确";
            return BuildErrorResult(HttpStatusCode.BadRequest, response);
        }
Esempio n. 2
0
        public HttpResponseMessage LoginForApp(LoginRequest request)
        {
            HttpResponseMessage responseMessage = new HttpResponseMessage();
            LoginResponse       response        = new LoginResponse();
            var formatter = new JsonMediaTypeFormatter();

            formatter.SerializerSettings =
                new Newtonsoft.Json.JsonSerializerSettings
            {
                NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
            };
            if (request == null)
            {
                response.IsSuccess         = false;
                response.Result            = "请求的数据不能为空";
                responseMessage.StatusCode = HttpStatusCode.BadRequest;

                responseMessage.Content = new ObjectContent <LoginResponse>(response, formatter);

                return(responseMessage);
            }
            if (String.IsNullOrWhiteSpace(request.Username))
            {
                responseMessage.StatusCode = HttpStatusCode.BadRequest;
                response.IsSuccess         = false;
                response.Result            = "用户名不能为空";
                responseMessage.Content    = new ObjectContent <LoginResponse>(response, formatter, "application/json");
                return(responseMessage);
            }


            var username = request.Username;

            if (Dependency.Resolve <IAuthenticationService>().Validate(ref username, request.Password))
            {
                CheckTwoFactorAuthentication(username, request);

                WebSecurityHelper.SetAuthenticationTicket(username, false);
                response.IsSuccess         = true;
                response.Result            = "登录成功";
                responseMessage.StatusCode = HttpStatusCode.OK;
                responseMessage.Content    = new ObjectContent <LoginResponse>(response, formatter);
                return(responseMessage);
            }

            response.IsSuccess         = false;
            response.Result            = "用户名或密码不正确";
            responseMessage.StatusCode = HttpStatusCode.BadRequest;
            responseMessage.Content    = new ObjectContent <LoginResponse>(response, formatter);
            return(responseMessage);
        }
Esempio n. 3
0
        public Result <ServiceResponse> Login(LoginRequest request)
        {
            return(this.ExecuteMethod(() =>
            {
                request.CheckNotNull();

                if (string.IsNullOrEmpty(request.Username))
                {
                    throw new ArgumentNullException("username");
                }

                var username = request.Username;

                if (Dependency.Resolve <IAuthenticationService>().Validate(ref username, request.Password))
                {
                    CheckTwoFactorAuthentication(username, request);

                    WebSecurityHelper.SetAuthenticationTicket(username, false);
                    return new ServiceResponse();
                }

                throw new ValidationError("AuthenticationError", Texts.Validation.AuthenticationError);
            }));
        }