예제 #1
0
        private static void SeedFriendlyUsers(FriendlyContext context)
        {
            var userStore   = new UserStore <FriendlyUser>(context);
            var userManager = new UserManager <FriendlyUser>(userStore);
            var firstUser   = new FriendlyUser()
            {
                UserName = "******", Email = "*****@*****.**"
            };

            if (!(context.Users.Any(u => u.UserName == firstUser.UserName)))
            {
                userManager.Create(firstUser, "Huzzah1982");
            }
            var secondUser = new FriendlyUser()
            {
                UserName = "******", Email = "*****@*****.**"
            };

            if (!(context.Users.Any(u => u.UserName == secondUser.UserName)))
            {
                userManager.Create(secondUser, "stlukes");
            }
            var thirdUser = new FriendlyUser()
            {
                UserName = "******", Email = "*****@*****.**"
            };

            if (!(context.Users.Any(u => u.UserName == thirdUser.UserName)))
            {
                userManager.Create(thirdUser, "63Princes");
            }
        }
예제 #2
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));
            }

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

            bool hasRegistered = user != null;

            if (hasRegistered)
            {
                Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);

                ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager,
                                                                                    OAuthDefaults.AuthenticationType);

                ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager,
                                                                                     CookieAuthenticationDefaults.AuthenticationType);

                AuthenticationProperties properties = FriendlyOAuthProvider.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());
        }
예제 #3
0
        public async Task <IHttpActionResult> Register(string username, string email, string password)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = new FriendlyUser()
            {
                UserName = username, Email = email
            };

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

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

            return(Ok(true));
        }
예제 #4
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var userManager = context.OwinContext.GetUserManager <FriendlyUserManager>();

            FriendlyUser 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 user.GenerateUserIdentityAsync(userManager,
                                                                                OAuthDefaults.AuthenticationType);

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

            AuthenticationProperties properties = CreateProperties(user.UserName);
            AuthenticationTicket     ticket     = new AuthenticationTicket(oAuthIdentity, properties);

            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }