Пример #1
0
        /// <summary>
        /// Renders information received from authentication service.
        /// </summary>
        public ActionResult GoogleAuth(string code, string error) //TODO refactor this
        {
            UserInfo userInfo;

            try
            {
                userInfo = googleClient.GetUserInfo(googleClient.GetAccessToken(code, error));
            }
            catch
            {
                return(RedirectToAction("LogOn"));
            }

            var user = repository.Query <User>(x => x.RelatedPerson, x => x.Teams)
                       .SingleOrDefault(x => x.GoogleId == userInfo.Id);

            if (null == user)
            {
                user = new User
                {
                    GoogleId      = userInfo.Id,
                    RelatedPerson = repository.Query <Person>().SingleOrDefault(x => x.Email == userInfo.Email)
                };
                if (null == user.RelatedPerson)
                {
                    var person = new Person
                    {
                        Email        = userInfo.Email,
                        FirstName    = userInfo.FirstName,
                        LastName     = userInfo.LastName,
                        PhotoUri     = userInfo.PhotoUri,
                        Role         = PersonRole.Employee,
                        CreationDate = DateTime.Now
                    };
                    user.RelatedPerson = person;
                    repository.Save(person);
                }
                repository.Save(user);
            }

            appContext.User = user;

            return(RedirectToRoute("Default"));
        }
Пример #2
0
        public ActionResult Authorize(string state, string code, string error)
        {
            OAuthState authState = this.stateManager.GetState(state);

            if (authState == null)
            {
                throw new InvalidOperationException("Invalid Authorization State");
            }

            UrlBuilder errorUrlBuilder = new UrlBuilder(authState.FailureUrl);

            if (string.IsNullOrWhiteSpace(error))
            {
                GoogleClient client = new GoogleClient(this.context.Config.Social.Google.AppID, this.context.Config.Social.Google.AppSecret);

                var credential = client.GetAccessToken(code, SocialApiSetting.BuildUrl(this.context.Config.Social.Google.Domain, "social/google/authorize"));
                if (credential != null && credential.Success)
                {
                    UrlBuilder redirectBuilder = new UrlBuilder(authState.SuccessUrl);
                    redirectBuilder.QueryString.Add("token", credential.Token);
                    redirectBuilder.QueryString.Add("refreshToken", credential.RefreshToken);
                    redirectBuilder.QueryString.Add("expiresIn", credential.ExpiresIn.ToStringValue());

                    if (!string.IsNullOrWhiteSpace(authState.State))
                    {
                        redirectBuilder.QueryString.Add("state", authState.State);
                    }

                    return(new RedirectResult(redirectBuilder.ToString()));
                }

                if (credential != null && !string.IsNullOrWhiteSpace(credential.ErrorCode))
                {
                    errorUrlBuilder.QueryString.Add("code", credential.ErrorCode);
                    errorUrlBuilder.QueryString.Add("message", credential.ErrorMessage);
                }
            }
            errorUrlBuilder.QueryString.Add("code", error);


            return(new RedirectResult(errorUrlBuilder.ToString()));
        }