Exemplo n.º 1
0
        public async Task <CredentialVerificationResult> VerifyCredentialAsync(NetworkCredential credential, CancellationToken cancelToken)
        {
            if (credential.IsEmpty())
            {
                return(CredentialVerificationResult.NoCredential);
            }
            if (credential.IsInvalid())
            {
                return(CredentialVerificationResult.SpellingError);
            }

            // check network

            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                return(CredentialVerificationResult.NetworkError);
            }

            // check web-service

            var webServiceIsUpAndRunning = await ServiceWrapper.CheckAsync();

            if (webServiceIsUpAndRunning == false)
            {
                return(CredentialVerificationResult.WebServiceDown);
            }

            using (var wrapper = new ServiceWrapper(cancelToken))
            {
                var service = wrapper.Service;
                // check web-service call

                try
                {
                    var posts = await service.GetNewDataAsync(new ChangeRequest
                    {
                        userName           = credential.UserName,
                        password           = credential.Password,
                        maxOutput          = 0,
                        messageRowVersion  = new byte[8],
                        ratingRowVersion   = new byte[8],
                        moderateRowVersion = new byte[8],
                        subscribedForums   = new RequestForumInfo[]
                        {
                            new RequestForumInfo
                            {
                                forumId        = 1,
                                isFirstRequest = true
                            }
                        }
                    });

                    if (posts.Body.GetNewDataResult.userId == 0)
                    {
                        return(CredentialVerificationResult.WrongCredential);
                    }
                    else
                    {
                        var userId = posts.Body.GetNewDataResult.userId;

                        var users = await service.GetUserByIdsAsync(new UserByIdsRequest
                        {
                            userName = credential.UserName,
                            password = credential.Password,
                            userIds  = new ArrayOfInt {
                                userId
                            },
                        });

                        var user = users.Body.GetUserByIdsResult.users.FirstOrDefault();
                        if (user == null)
                        {
                            return(CredentialVerificationResult.WrongCredential);
                        }

                        this.User = new UserModel
                        {
                            Id        = user.userId,
                            Alias     = user.userNick,
                            Name      = user.userName,
                            RealName  = user.realName,
                            Origin    = user.whereFrom,
                            Address   = user.homePage,
                            Email     = user.publicEmail,
                            Interests = user.specialization,
                            Signature = user.origin,
                        };
                        StoreUser();
                    }
                }
                catch (Exception)
                {
                    return(CredentialVerificationResult.WrongCredential);
                }
            }

            return(CredentialVerificationResult.Success);
        }