/// <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); } }
/// <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); }
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); }
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); }
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); }
/// <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); }
private static string Serialize(CookieUserData userData) { return(JsonConvert.SerializeObject(userData)); }
/// <summary> /// Выход из системы /// </summary> public void Logout() { CookieDataProvider.Logout(); _cookieUserData = null; }