public void test()
        {
            var cont = new SoLoudContext();

            ApplicationUser asdok = new ApplicationUser();

            cont.Users.Add(asdok);
        }
Пример #2
0
        public async Task VerifyPost(string PostId)
        {
            var db = new SoLoudContext();

            Post post = db.Posts.FirstOrDefault(x => x.Id == PostId);

            if (post == null)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent("No Post with such id")
                });
            }
            else if (post.PostStatus.Equals(PostStatus.Verified))
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent("Post already verified")
                });
            }

            var user = db.Users.Include("Claims").FirstOrDefault(x => x.Id == post.UserId);

            if (user == null)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.ExpectationFailed)
                {
                    Content = new StringContent(String.Format("Post is connected to user with id {0} but no such user was found", post.UserId))
                });
            }

            var FbClaim = user.Claims.FirstOrDefault(x => x.ClaimType == SoloudClaimTypes.FacebookAccessToken.ToString() && x.UserId == user.Id);

            if (FbClaim == null)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.ExpectationFailed)
                {
                    Content = new StringContent(String.Format("No saved fb token was found for user with id {0}", user.Id))
                });
            }

            var EncryptedFbToken = FbClaim.ClaimValue;
            var DecryptedFbToken = AESThenHMAC.SimpleDecryptWithPassword(EncryptedFbToken, EncryptionKey);

            var fbClient = new FbClient(DecryptedFbToken);

            var fbResponse = await fbClient.MultiphotoStory(post.Photos.ToList(), post.Text, post.PlaceId);

            post.VerifiedAt = DateTimeOffset.Now;
            //post.IsVerified = true;
            post.PostStatus = PostStatus.Verified;
            post.FacebookId = fbResponse.id;
            db.SaveChanges();
        }
        internal async Task <ApplicationUser> CreateUser(string UserType, string email, string username, string hometown)
        {
            var context  = new SoLoudContext();
            var UserRole = context.Roles.FirstOrDefault(x => x.Name == UserType);

            var user = new ApplicationUser {
                UserName = username, Email = email, Hometown = hometown
            };

            user.Roles.Add(new IdentityUserRole()
            {
                RoleId = UserRole.Id, UserId = user.Id
            });

            var result = await UserManager.CreateAsync(user);

            return(UserManager.FindById(user.Id));
        }
        private async Task GrantResourceOwnerCredentialsFromFacebookToken(OAuthGrantResourceOwnerCredentialsContext context)
        {
            //Find External Token
            var facebookToken = getExternalToken(context);

            if (facebookToken == null)
            {
                throw new Exception("ExternalToken is null");
            }

            //Get User Using FacebookToken
            var fb = new FacebookClient(facebookToken);

            Facebook.Me me = fb.Get <Facebook.Me>("me", new { fields = "id, name, email, gender, birthday, picture.type(large)" });

            var User = userManager.FindByEmail(me.email);

            if (User == null)
            {
                var AccCtrl = new AccountController(HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>(), HttpContext.Current.GetOwinContext().GetUserManager <ApplicationSignInManager>());
                User = await AccCtrl.CreateUser("User", me.email, me.email, null);
            }

            //Save fbtoken to db
            var db = new SoLoudContext();

            //We need to refetch from db in order to be able to edit/add claims. If we dont the context is not tracking the Claim entities and changes will not save
            User = db.Users.Include("Claims").FirstOrDefault(x => x.Id == User.Id);
            var fbTokenClaim = User.Claims.FirstOrDefault(x => x.ClaimType == SoloudClaimTypes.FacebookAccessToken.ToString());

            if (fbTokenClaim == null)
            {
                fbTokenClaim = new Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim()
                {
                    ClaimType = SoloudClaimTypes.FacebookAccessToken.ToString()
                };
                User.Claims.Add(fbTokenClaim);
            }
            fbTokenClaim.ClaimValue = AESThenHMAC.SimpleEncryptWithPassword(facebookToken, ConfigurationManager.AppSettings["EncryptionKey"].ToString());
            db.SaveChanges();

            //Create Token and return
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            identity.AddClaim(new Claim("UserName", User.UserName));
            identity.AddClaim(new Claim("UserId", User.Id));
            identity.AddClaim(new Claim(SoloudClaimTypes.FacebookAccessToken.ToString(), facebookToken));

            //find user roles
            var UserRoles = User.Roles.Join(db.Roles, x => x.RoleId, r => r.Id, (x, r) => r.Name);

            if (UserRoles != null && UserRoles.Count() > 0)
            {
                identity.AddClaim(new Claim("Roles", UserRoles.Aggregate((acc, cur) => acc += "," + cur)));
            }

            var props = new AuthenticationProperties(new Dictionary <string, string>
            {
                { "User", JsonConvert.SerializeObject(User) }
            });

            var ticket = new AuthenticationTicket(identity, props);

            context.Validated(ticket);

            return;
        }