public void SignIn(AppIdentity user, bool isPersistent) { var now = DateTime.UtcNow.ToLocalTime(); string userData = string.Format("{0};{1};{2}", user.Id, user.Name, user.Email); var ticket = new FormsAuthenticationTicket( 1, user.Email, now, now.Add(FormsAuthentication.Timeout), isPersistent, userData, FormsAuthentication.FormsCookiePath); var encryptedTicket = FormsAuthentication.Encrypt(ticket); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); cookie.HttpOnly = true; if (ticket.IsPersistent) { cookie.Expires = ticket.Expiration; } cookie.Secure = FormsAuthentication.RequireSSL; cookie.Path = FormsAuthentication.FormsCookiePath; if (FormsAuthentication.CookieDomain != null) { cookie.Domain = FormsAuthentication.CookieDomain; } _httpContext.Response.Cookies.Add(cookie); _cachedUser = user; }
public AppIdentity GetAuthenticatedUser() { if (_cachedUser != null) return _cachedUser; if (!(_httpContext.User.Identity is FormsIdentity)) return null; var formsIdentity = (FormsIdentity)_httpContext.User.Identity; string[] userdata = formsIdentity.Ticket.UserData.Split(';'); string email = formsIdentity.Ticket.Name; string id = userdata[0]; string name = userdata[1]; var user = new AppIdentity(id, name, email); _cachedUser = user; return _cachedUser; }
public async Task<ActionResult> SignIn(LoginModel model, string returnUrl) { if (ModelState.IsValid) { var request = new LoginRequest(); request.Password = model.Password; request.Username = model.Email; var response = await _userProxy.GetLoginInformation(request); if (response.Ack == AckType.SUCCESS) { var data = response.Data; var identity = new AppIdentity(data.UserId.ToString(), data.Name, data.Email); _authenticationService.SignIn(identity, true); return RedirectToLocal(returnUrl); } } ModelState.AddModelError("", "Username or password incorret."); return View(model); }
public async Task<ActionResult> FacebookSignInCallback(string code, string returnUrl) { var facebookClient = new FacebookClient(); var redirectUri = BuildFacebookSignInCallbackUrl(); dynamic result = facebookClient.Post("oauth/access_token", new { client_id = _configurationService.FacebookAppId, client_secret = _configurationService.FacebookAppSecret, redirect_uri = redirectUri, code = code }); var access_token = result.access_token; facebookClient.AccessToken = access_token; dynamic me = facebookClient.Get("me"); string email = me.email; string name = me.name; string id = me.id; var userId = Guid.NewGuid(); var command = new RegisterExternalUserCommand(userId, name, email, FACEBOOK_PROVIDER, id); var response = await _userProxy.RegisterExternalUserIfNeeded(command); if (response.Ack == AckType.SUCCESS) { var appIdentity = new AppIdentity(userId.ToString(), command.Name, command.Email); _authenticationService.SignIn(appIdentity, true); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("", response.Message); PreserveTempData(); return RedirectToAction("SignIn", new { returnUrl = returnUrl }); } }
public async Task<ActionResult> Register(RegisterModel model, string returnUrl) { if (!ModelState.IsValid) { return View(model); } var userId = Guid.NewGuid(); var command = new RegisterUserCommand(userId, model.Name, model.Email, model.Password); var response = await _userProxy.Register(command); if (response.Ack == AckType.SUCCESS) { var appIdentity = new AppIdentity(userId.ToString(), command.Name, command.Email); _authenticationService.SignIn(appIdentity, true); return RedirectToLocal(returnUrl); } return View(model); }