Example #1
0
        /// <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);
        }
        /// <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);
        }
 /// <summary>
 /// Выход из системы
 /// </summary>
 public void Logout()
 {
     CookieDataProvider.Logout();
     _cookieUserData = null;
 }