コード例 #1
0
ファイル: AccountController.cs プロジェクト: eletol/XYZ
        public async Task <IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var info = await Authentication.GetExternalLoginInfoAsync();

            if (info == null)
            {
                return(InternalServerError());
            }

            var user = new User()
            {
                UserName = model.Email, Email = model.Email
            };

            IdentityResult result = await UserManager.CreateAsync(user);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            result = await UserManager.AddLoginAsync(user.Id, info.Login);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }
            return(Ok());
        }
コード例 #2
0
ファイル: AccountController.cs プロジェクト: eletol/XYZ
        public async Task <IHttpActionResult> FacebookRegister(RegisterExternalBindingModel3 model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.Token))
                {
                    return(BadRequest("Invalid OAuth access token"));
                }

                var tokenExpirationTimeSpan = TimeSpan.FromDays(360);
                // Get the fb access token and make a graph call to the /me endpoint
                var fbUser = await VerifyFacebookAccessToken(model.Token);

                if (fbUser?.Email == null)
                {
                    return(BadRequest("Invalid OAuth access token"));
                }

                // Check if the user is already registered
                var user = await UserManager.FindByEmailAsync(fbUser.Email);

                // If not, register it
                if (user == null)
                {
                    var userPassword   = "******" + fbUser.ID.ToString();
                    var randomPassword = System.Web.Security.Membership.GeneratePassword(10, 0) + "1Ds@";
                    user = new User()
                    {
                        UserName = fbUser.Email, MobileNumber = model.PhoneNumber, Email = fbUser.Email, Name = string.IsNullOrWhiteSpace(fbUser.Name) ? model.Name : fbUser.Name, PhoneNumber = model.PhoneNumber, Photo = model.Photo, CountryCode = model.CountryCode
                    };
                    user.Id = Guid.NewGuid().ToString();
                    IdentityResult result = await UserManager.CreateAsync(user, userPassword + randomPassword);

                    if (!result.Succeeded)
                    {
                        return(GetErrorResult(result));
                    }


                    IdentityResult roleResult;
                    bool           adminRoleExists = await AppRoleManager.RoleExistsAsync("User");

                    if (!adminRoleExists)
                    {
                        roleResult = await AppRoleManager.CreateAsync(new RoleForUser()
                        {
                            Id   = Guid.NewGuid().ToString(),
                            Name = "User"
                        });
                    }

                    var userResult = await UserManager.AddToRoleAsync(user.Id, "User");
                }
                return(Ok(GenerateLocalAccessTokenResponse(user.UserName, user.Id)));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
コード例 #3
0
ファイル: AccountController.cs プロジェクト: eletol/XYZ
        public async Task <IHttpActionResult> TwitterRegister(RegisterExternalBindingModel2 model)
        {
            if (string.IsNullOrEmpty(model.AccessToken) || string.IsNullOrEmpty(model.AccessTokenSecret))
            {
                return(BadRequest("Invalid OAuth access token"));
            }
            Auth.SetUserCredentials(ConfigurationManager.AppSettings["Twitter.ConsumerKey"], ConfigurationManager.AppSettings["Twitter.ConsumerSecretKey"], model.AccessToken, model.AccessTokenSecret);
            var twUser = Tweetinvi.User.GetAuthenticatedUser();

            var tokenExpirationTimeSpan = TimeSpan.FromDays(360);

            // Get the fb access token and make a graph call to the /me endpoint
            if (twUser?.Email == null)
            {
                return(BadRequest("invalid token"));
            }
            // Check if the user is already registered
            var user = await UserManager.FindByEmailAsync(twUser.Email);

            // If not, register it
            if (user == null)
            {
                var randomPassword = System.Web.Security.Membership.GeneratePassword(10, 0) + "1Ds@";

                user = new User()
                {
                    UserName = twUser.Email, Email = twUser.Email, Name = twUser.Name.IsNullOrWhiteSpace() ? model.Name : twUser.Name, PhoneNumber = model.PhoneNumber, Photo = twUser.ProfileImageUrl, CountryCode = model.CountryCode
                };

                IdentityResult result = await UserManager.CreateAsync(user, randomPassword);

                if (!result.Succeeded)
                {
                    return(GetErrorResult(result));
                }
                IdentityResult roleResult;
                bool           adminRoleExists = await AppRoleManager.RoleExistsAsync("User");

                if (!adminRoleExists)
                {
                    roleResult = await AppRoleManager.CreateAsync(new RoleForUser()
                    {
                        Name = "User"
                    });
                }

                var userResult = await UserManager.AddToRoleAsync(user.Id, "User");
            }
            return(Ok(GenerateLocalAccessTokenResponse(user.UserName, user.Id)));
        }
コード例 #4
0
ファイル: AccountController.cs プロジェクト: eletol/XYZ
        public async Task <IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = new User()
            {
                UserName = model.Email, Email = model.Email
            };

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            return(Ok());
        }