public async Task<JsonResult> Authenticate() { var profile = await TryAuthenticateFromHttpContext(); if (profile != null) { _baseModel.User = profile; return Json(new { Status = "Connected", Session = new { User = SessionWrapper.Get<string>("CurrentUserProfileName") }, }, JsonRequestBehavior.AllowGet); } var svc = new LiveIdAuth(); var url = svc.GetLogoutUrl("http://" + Request.Headers.Get("host")); SessionWrapper.Clear(); return Json(new { Status = "unknown", S = url }, JsonRequestBehavior.AllowGet); }
public async Task<JsonResult> Authenticate() { var result = await TryAuthenticateFromHttpContext(_communityService, _notificationService); if (result.Status == LiveConnectSessionStatus.Connected) { _baseModel.User = SessionWrapper.Get<ProfileDetails>("ProfileDetails"); return Json(new { Status = result.Status.ToString(), Session = new { result.Session.AccessToken, result.Session.AuthenticationToken, Expires = result.Session.Expires.ToLocalTime().ToString(), result.Session.RefreshToken, result.Session.Scopes, User = SessionWrapper.Get<string>("CurrentUserProfileName") }, }, JsonRequestBehavior.AllowGet); } var svc = new LiveIdAuth(); var url = svc.GetLogoutUrl("http://" + Request.Headers.Get("host")); SessionWrapper.Clear(); return Json(new { Status = result.Status.ToString(), S = url }, JsonRequestBehavior.AllowGet); }
public async Task<bool> RegisterUser() { var profileDetails = await ValidateAuthentication(); if (profileDetails == null) { var svc = new LiveIdAuth(); dynamic jsonResult = svc.GetMeInfo(System.Web.HttpContext.Current.Request.Headers["LiveUserToken"]); profileDetails = new ProfileDetails(jsonResult); // While creating the user, IsSubscribed to be true always. profileDetails.IsSubscribed = true; // When creating the user, by default the user type will be of regular. profileDetails.UserType = UserTypes.Regular; profileDetails.ID = ProfileService.CreateProfile(profileDetails); // This will used as the default community when user is uploading a new content. // This community will need to have the following details: var communityDetails = new CommunityDetails { CommunityType = CommunityTypes.User,// 1. This community type should be User CreatedByID = profileDetails.ID,// 2. CreatedBy will be the new USER. IsFeatured = false,// 3. This community is not featured. Name = Resources.UserCommunityName,// 4. Name should be NONE. AccessTypeID = (int) AccessType.Private,// 5. Access type should be private. CategoryID = (int) CategoryType.GeneralInterest// 6. Set the category ID of general interest. We need to set the Category ID as it is a foreign key and cannot be null. }; // 7. Create the community _communityService.CreateCommunity(communityDetails); // Send New user notification. _notificationService.NotifyNewEntityRequest(profileDetails, HttpContext.Request.Url.GetServerLink()); } else { throw new WebFaultException<string>("User already registered", HttpStatusCode.BadRequest); } return true; }
protected static async Task<ProfileDetails> ValidateAuthentication() { var svc = new LiveIdAuth(); var token = System.Web.HttpContext.Current.Request.Headers["LiveUserToken"]; if (token == null) { token = System.Web.HttpContext.Current.Request.QueryString["LiveUserToken"]; } var cachedProfile = ProfileCacheManager.GetProfileDetails(token); if (cachedProfile!=null) { return cachedProfile; } var userId = await svc.GetUserId(token); if (userId != null && userId.Length > 3) { var profileService = DependencyResolver.Current.GetService(typeof(IProfileService)) as IProfileService; var profileDetails = profileService.GetProfile(userId); if (profileDetails != null) { ProfileCacheManager.CacheProfile(token,profileDetails); } return profileDetails; } return null; }
protected async Task<LiveLoginResult> TryAuthenticateFromHttpContext(ICommunityService communityService, INotificationService notificationService) { var svc = new LiveIdAuth(); var result = await svc.Authenticate(); if (result.Status == LiveConnectSessionStatus.Connected) { var client = new LiveConnectClient(result.Session); SessionWrapper.Set("LiveConnectClient", client); SessionWrapper.Set("LiveConnectResult", result); SessionWrapper.Set("LiveAuthSvc", svc); var getResult = await client.GetAsync("me"); var jsonResult = getResult.Result as dynamic; var profileDetails = ProfileService.GetProfile(jsonResult.id); if (profileDetails == null) { profileDetails = new ProfileDetails(jsonResult); // While creating the user, IsSubscribed to be true always. profileDetails.IsSubscribed = true; // When creating the user, by default the user type will be of regular. profileDetails.UserType = UserTypes.Regular; profileDetails.ID = ProfileService.CreateProfile(profileDetails); // This will used as the default community when user is uploading a new content. // This community will need to have the following details: var communityDetails = new CommunityDetails { CommunityType = CommunityTypes.User, // 1. This community type should be User CreatedByID = profileDetails.ID, // 2. CreatedBy will be the new USER. IsFeatured = false, // 3. This community is not featured. Name = Resources.UserCommunityName, // 4. Name should be NONE. AccessTypeID = (int) AccessType.Private, // 5. Access type should be private. CategoryID = (int) CategoryType.GeneralInterest // 6. Set the category ID of general interest. We need to set the Category ID as it is a foreign key and cannot be null. }; // 7. Create the community communityService.CreateCommunity(communityDetails); // Send New user notification. notificationService.NotifyNewEntityRequest(profileDetails, HttpContext.Request.Url.GetServerLink()); } SessionWrapper.Set<long>("CurrentUserID", profileDetails.ID); SessionWrapper.Set<string>("CurrentUserProfileName", profileDetails.FirstName + " " + profileDetails.LastName); SessionWrapper.Set("ProfileDetails", profileDetails); SessionWrapper.Set("AuthenticationToken", result.Session.AuthenticationToken); } return result; }
public ActionResult Logout() { var svc = new LiveIdAuth(); var url = svc.GetLogoutUrl("http://" + Request.Headers.Get("host")); SessionWrapper.Clear(); var refreshTokenCookie = Response.Cookies["refresh_token"]; var accessTokenCookie = Response.Cookies["access_token"]; if (refreshTokenCookie != null && !string.IsNullOrEmpty(refreshTokenCookie.Value)) { refreshTokenCookie.Expires = DateTime.Now.AddDays(-1); Response.Cookies.Add(refreshTokenCookie); } if (accessTokenCookie != null && !string.IsNullOrEmpty(accessTokenCookie.Value)) { accessTokenCookie.Expires = DateTime.Now.AddDays(-1); Response.Cookies.Add(accessTokenCookie); } return Redirect(url); }