Пример #1
0
        public dynamic Process(NancyModule nancyModule, AuthenticateCallbackData model)
        {
            var currentUserId = nancyModule.GetUserID();

            if (model.Exception != null)
            {
                _logger.Error("SOCIALAUTH", model.Exception);
                return(nancyModule.Response.AsRedirect("/"));
            }

            var userInfo     = model.AuthenticatedClient.UserInformation;
            var providerName = model.AuthenticatedClient.ProviderName;
            var userName     = userInfo.UserName ?? userInfo.Name ?? "Unknown";
            var email        = userInfo.Email ?? string.Empty;

            var user = _userService.GetByIdentity(providerName, userInfo.Id);

            if (user == null)
            {
                if (currentUserId == null)
                {
                    user = _userService.AddUser(userName, email, providerName, userInfo.Id);
                }
                else
                {
                    user = _userService.AttachIdentity(currentUserId, userName, email, providerName, userInfo.Id);
                }
            }
            else
            {
                if (currentUserId != null && user.ID != currentUserId)
                {
                    nancyModule.Session["MergeID"] = user.ID;

                    return(nancyModule.Response.AsRedirect("/#!/profile?tab=logins"));
                }
            }

            return(nancyModule.LoginAndRedirect(user.Identifier, DateTime.Now.AddMonths(3), model.ReturnUrl));
        }