예제 #1
0
        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);
        }
        protected async Task <ProfileDetails> TryAuthenticateFromHttpContext()
        {
            if (SessionWrapper.Get <ProfileDetails>("ProfileDetails") != null)
            {
                return(SessionWrapper.Get <ProfileDetails>("ProfileDetails"));
            }

            var svc = new LiveIdAuth();

            var result = await svc.Authenticate();

            string userId = null;

            if (result.Status != LiveConnectSessionStatus.Connected)
            {
                var resultstring = await svc.RefreshTokens();

                if (string.IsNullOrEmpty(resultstring))
                {
                    return(null);
                }
                var tokens = new { access_token = "", refresh_token = "" };
                var json   = JsonConvert.DeserializeAnonymousType(resultstring, tokens);
                userId = await svc.GetUserId(tokens.access_token);

                if (string.IsNullOrEmpty(userId))
                {
                    return(null);
                }
                return(await InitUserProfile(userId, json.access_token));
            }

            var     client     = new LiveConnectClient(result.Session);
            dynamic jsonResult = null;
            var     getResult  = await client.GetAsync("me");

            jsonResult = getResult.Result as dynamic;
            foreach (KeyValuePair <string, object> item in jsonResult)
            {
                if (item.Key.ToLower() == "id")
                {
                    userId = item.Value.ToString();
                }
            }
            //userId = jsonResult["id"].ToString();

            return(await InitUserProfile(userId, result.Session.AccessToken));
        }
예제 #3
0
        protected async Task <ProfileDetails> TryAuthenticateFromHttpContext()
        {
            if (Request.Headers.Get("host").Contains("localhost"))
            {
                return(null);
            }
            if (SessionWrapper.Get <ProfileDetails>("ProfileDetails") != null)
            {
                return(SessionWrapper.Get <ProfileDetails>("ProfileDetails"));
            }

            var svc = new LiveIdAuth();

            var profile = await TryRefreshToken(svc);

            if (profile != null)
            {
                return(profile);
            }
            var result = await svc.Authenticate();

            string userId = null;

            if (result.Status != LiveConnectSessionStatus.Connected)
            {
                return(await UserFromToken(await svc.RefreshTokens(), svc));
            }

            var     client     = new LiveConnectClient(result.Session);
            dynamic jsonResult = null;
            var     getResult  = await client.GetAsync("me");

            jsonResult = getResult.Result;
            foreach (KeyValuePair <string, object> item in jsonResult)
            {
                if (item.Key.ToLower() == "id")
                {
                    userId = item.Value.ToString();
                }
            }
            //userId = jsonResult["id"].ToString();

            return(await InitUserProfile(userId, result.Session.AccessToken));
        }