コード例 #1
0
        public async Task<IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            ApplicationUser user = new ApplicationUser
            {
                UserName = model.Email,
                Email = model.Email,
                FirstName = model.FirstName,
                LastName = model.LastName
            };

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

            if (errorResult != null)
            {
                return errorResult;
            }

            MailSendingFacade mailSender = new MailSendingFacade();
            mailSender.SendNewUserRegisteredMail(model.Email, model.FirstName, model.LastName);

            return Ok();
        }
コード例 #2
0
        public async Task<IHttpActionResult> GetExternalLogin(string provider, string error = null)
        {
            if (error != null)
            {
                return Redirect(Url.Content("~/") + "?code=0&data=" + Uri.EscapeDataString(error) + "#/autherror");
            }

            if (!User.Identity.IsAuthenticated)
            {
                return new ChallengeResult(provider, this);
            }

            ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);

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

            if (externalLogin.LoginProvider != provider)
            {
                Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                return new ChallengeResult(provider, this);
            }

            ApplicationUser user = await UserManager.FindByNameAsync(externalLogin.Email);
            if (user == null)
            {
                user = new ApplicationUser()
                {
                    UserName = externalLogin.Email,
                    Email = externalLogin.Email,
                    FirstName = externalLogin.FirstName,
                    LastName = externalLogin.LastName
                };

                user.Logins.Add(new IdentityUserLogin()
                {
                    LoginProvider = externalLogin.LoginProvider,
                    ProviderKey = externalLogin.ProviderKey,
                    UserId = user.Id
                });

                IdentityResult result = await UserManager.CreateAsync(user);
                IHttpActionResult errorResult = GetErrorResult(result);

                if (errorResult != null)
                {
                    return errorResult;
                }

                MailSendingFacade mailSender = new MailSendingFacade();
                mailSender.SendNewUserRegisteredMail(externalLogin.Email, externalLogin.FirstName, externalLogin.LastName);
            }
            else
            {
                bool isExistingLogin = user.Logins.Any(l => l.LoginProvider == externalLogin.LoginProvider && l.ProviderKey == externalLogin.ProviderKey);
                if (!isExistingLogin)
                {
                    user.Logins.Add(new IdentityUserLogin()
                    {
                        LoginProvider = externalLogin.LoginProvider,
                        ProviderKey = externalLogin.ProviderKey,
                        UserId = user.Id
                    });

                    await UserManager.UpdateAsync(user);
                }
            }

            Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            ClaimsIdentity oAuthIdentity = await UserManager.CreateIdentityAsync(user,
                OAuthDefaults.AuthenticationType);
            ClaimsIdentity cookieIdentity = await UserManager.CreateIdentityAsync(user,
                CookieAuthenticationDefaults.AuthenticationType);
            //AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user);
            Authentication.SignIn(/*properties, */oAuthIdentity, cookieIdentity);

            return Ok();
        }