コード例 #1
0
ファイル: OAuthService.cs プロジェクト: FatkullinT/NewsPortal
 /// <summary>
 /// Поиск существующего и создание нового пользователя
 /// </summary>
 /// <param name="cookieUserData"></param>
 /// <param name="newUser"></param>
 /// <returns></returns>
 private User FindOrCreateUser(CookieUserData cookieUserData, out bool newUser)
 {
     using (IUnitOfWork unitOfWork = _unitOfWorkFactory.Create())
     {
         OAuthMembership oAuthMembership =
             _oAuthMembershipRepository.GetWithUser()
             .FirstOrDefault(
                 m =>
                 m.OAuthUserId == cookieUserData.OAuthUserId &&
                 m.ProviderName == cookieUserData.OAuthProviderName);
         if (oAuthMembership != null)
         {
             newUser = false;
             return(oAuthMembership.User);
         }
         User user = new User();
         user.Name = cookieUserData.UserName;
         OAuthMembership membership = new OAuthMembership();
         membership.User         = user;
         membership.ProviderName = cookieUserData.OAuthProviderName;
         membership.OAuthUserId  = cookieUserData.OAuthUserId;
         _userRepository.Create(user);
         _oAuthMembershipRepository.Create(membership);
         unitOfWork.Commit();
         newUser = true;
         return(user);
     }
 }
コード例 #2
0
ファイル: OAuthService.cs プロジェクト: FatkullinT/NewsPortal
        /// <summary>
        /// Аутентификация по полученному токену
        /// </summary>
        /// <returns></returns>
        public OAuthLoginResponse Login()
        {
            OAuthLoginResponse loginResponse = new OAuthLoginResponse();
            string             providerName  = HttpContext.Current.Request.Params["providerName"];
            OAuthProviderBase  oAuthProvider = Providers.FirstOrDefault(provider => string.Equals(provider.ProviderName, providerName));

            if (oAuthProvider == null)
            {
                loginResponse.Success = false;
            }
            else
            {
                NameValueCollection response = oAuthProvider.SendTokenRequest();
                string token = response["access_token"];
                if (string.IsNullOrEmpty(token))
                {
                    loginResponse.Success = false;
                }
                else
                {
                    DateTime       tokenExpires   = GetTokenExpires(response);
                    CookieUserData cookieUserData = oAuthProvider.GetUserData(token, out loginResponse.Account);
                    User           user           = FindOrCreateUser(cookieUserData, out loginResponse.IsNewUser);
                    cookieUserData.UserId = user.Id;
                    CookieDataProvider.Login(user.Name, cookieUserData, tokenExpires);
                    loginResponse.Success = true;
                }
            }

            return(loginResponse);
        }
コード例 #3
0
        public static void Login(string userName, CookieUserData userData, DateTime expiresDate)
        {
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                2,
                userName,
                DateTime.Now,
                expiresDate,
                true,
                Serialize(userData));
            string encryptedTicket = FormsAuthentication.Encrypt(ticket);

            SetCookie(AuthCookieName, encryptedTicket, expiresDate);
        }
コード例 #4
0
        public override CookieUserData GetUserData(string token, out Account account)
        {
            Uri    uri      = new Uri(string.Format("https://graph.facebook.com/me?access_token={0}&fields=name,email", token));
            string response = HttpRequestHelper.SendRequest(uri);
            Dictionary <string, string> facebookUserData =
                JsonConvert.DeserializeObject <Dictionary <string, string> >(response);
            CookieUserData cookieUserData = new CookieUserData();

            cookieUserData.OAuthProviderName = ProviderName;
            cookieUserData.OAuthToken        = token;
            cookieUserData.OAuthUserId       = facebookUserData["id"];
            cookieUserData.UserName          = facebookUserData["name"];
            account          = new Account();
            account.Email    = facebookUserData["email"];
            account.SendNews = false;
            return(cookieUserData);
        }
コード例 #5
0
        public static void Login(string userName, CookieUserData userData, bool rememberMe)
        {
            DateTime expiresDate = DateTime.Now.AddHours(2);

            if (rememberMe)
            {
                expiresDate = expiresDate.AddDays(10);
            }
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                2,
                userName,
                DateTime.Now,
                expiresDate,
                rememberMe,
                Serialize(userData));
            string encryptedTicket = FormsAuthentication.Encrypt(ticket);

            SetCookie(AuthCookieName, encryptedTicket, expiresDate);
        }
コード例 #6
0
        /// <summary>
        /// Вход в систему
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="persistCookie"></param>
        /// <returns></returns>
        public bool Login(string userName, string password, bool persistCookie)
        {
            Membership membership;

            using (_unitOfWorkFactory.Create())
            {
                membership = _membershipRepository.GetWithUser().FirstOrDefault(m => m.User.Name == userName);
            }
            if (membership != null && PasswordEncryptor.Validate(password, membership.Password))
            {
                _cookieUserData = new CookieUserData
                {
                    UserId   = membership.User.Id,
                    UserName = membership.User.Name
                };
                CookieDataProvider.Login(membership.User.Name, _cookieUserData, persistCookie);
                return(true);
            }
            return(false);
        }
コード例 #7
0
 private static string Serialize(CookieUserData userData)
 {
     return(JsonConvert.SerializeObject(userData));
 }
コード例 #8
0
 /// <summary>
 /// Выход из системы
 /// </summary>
 public void Logout()
 {
     CookieDataProvider.Logout();
     _cookieUserData = null;
 }