public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            try
            {
                using (UserManager <NhIdentityUser> userManager = _userManagerFactory())
                {
                    NhIdentityUser user = await userManager.FindAsync(context.UserName, context.Password);

                    if (user == null)
                    {
                        context.SetError("invalid_grant", "The user name or password is incorrect.");
                        return;
                    }

                    ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user,
                                                                                         context.Options.AuthenticationType);

                    ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user,
                                                                                           CookieAuthenticationDefaults.AuthenticationType);

                    AuthenticationProperties properties = CreateProperties(user.UserName);
                    AuthenticationTicket     ticket     = new AuthenticationTicket(oAuthIdentity, properties);
                    context.Validated(ticket);
                    context.Request.Context.Authentication.SignIn(cookiesIdentity);
                }
            }
            catch (Exception e)
            {
                throw;
            }
        }
Exemplo n.º 2
0
        private NhIdentityUser CreateDefaultUser(string userName)
        {
            var now  = DateTime.Now;
            var user = new NhIdentityUser
            {
                UserName                               = userName,
                Discriminator                          = "",
                ApplicationId                          = Guid.Empty,
                LoweredUserName                        = userName.ToLower(),
                IsAnonymous                            = false,
                LastActivityDate                       = now,
                IsApproved                             = true,
                IsLockedOut                            = false,
                CreateDate                             = now,
                LastLoginDate                          = now,
                LastPasswordChangedDate                = now,
                LastLockoutDate                        = now,
                FailedPasswordAttemptCount             = 5,
                FailedPasswordAttemptWindowStart       = now,
                FailedPasswordAnswerAttemptCount       = 5,
                FailedPasswordAnswerAttemptWindowStart = now
            };

            return(user);
        }
Exemplo n.º 3
0
        public async Task <IHttpActionResult> GetExternalLogin(string provider, string error = null)
        {
            if (error != null)
            {
                return(Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error)));
            }

            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));
            }

            NhIdentityUser user = await UserManager.FindAsync(new UserLoginInfo(externalLogin.LoginProvider,
                                                                                externalLogin.ProviderKey));

            bool hasRegistered = user != null;

            if (hasRegistered)
            {
                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.UserName);
                Authentication.SignIn(properties, oAuthIdentity, cookieIdentity);
            }
            else
            {
                IEnumerable <Claim> claims   = externalLogin.GetClaims();
                ClaimsIdentity      identity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType);
                Authentication.SignIn(identity);
            }

            return(Ok());
        }
Exemplo n.º 4
0
        public async Task <ManageInfoViewModel> GetManageInfo(string returnUrl, bool generateState = false)
        {
            NhIdentityUser user = await UserManager.FindByIdAsync(User.Identity.GetUserId());

            if (user == null)
            {
                return(null);
            }

            List <UserLoginInfoViewModel> logins = new List <UserLoginInfoViewModel>();

            foreach (var linkedAccount in user.Logins)
            {
                logins.Add(new UserLoginInfoViewModel
                {
                    LoginProvider = linkedAccount.LoginProvider,
                    ProviderKey   = linkedAccount.ProviderKey
                });
            }

            if (user.PasswordHash != null)
            {
                logins.Add(new UserLoginInfoViewModel
                {
                    LoginProvider = LocalLoginProvider,
                    ProviderKey   = user.UserName,
                });
            }

            return(new ManageInfoViewModel
            {
                LocalLoginProvider = LocalLoginProvider,
                UserName = user.UserName,
                Logins = logins,
                ExternalLoginProviders = GetExternalLogins(returnUrl, generateState)
            });
        }