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 TicTacToeUser() { 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()); }
public IActionResult Index() { TicTacToe ticTacToe = TicTacToe.Instance; ticTacToe.ClearBoard(); TicTacToeBot bot = TicTacToeBot.Instance; TicTacToeUser user = TicTacToeUser.Instance; bot.SetTicTacToe(ticTacToe); user.SetTicTacToe(ticTacToe); // switch starting if (ticTacToe.IsPlayerStarting == true) { ticTacToe.IsPlayerStarting = false; // bot first move AlphaBetaPruning alphaBetaPruning = new AlphaBetaPruning(ticTacToe.ticTacToeBoard, ticTacToe.Level); Tuple <int, int> xy = alphaBetaPruning.BestMove(); bot.MakeMove(xy.Item1, xy.Item2); } else { ticTacToe.IsPlayerStarting = true; } return(View(ticTacToe)); }
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)); } TicTacToeUser 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 = 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()); }
//[EnableCors(origins: "*", headers: "*", methods: "*")] public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = new TicTacToeUser { UserName = model.Username, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return(GetErrorResult(result)); } return(Ok()); }
public IActionResult NextMove(IFormCollection formCollection) { TicTacToe ticTacToe = TicTacToe.Instance; TicTacToeChecker ticTacToeChecker = new TicTacToeChecker(); TicTacToeBot bot = TicTacToeBot.Instance; TicTacToeUser user = TicTacToeUser.Instance; // setting level ticTacToe.Level = int.Parse(formCollection["Level"]); string place = formCollection["Button"]; int userX = int.Parse(place[0].ToString()); int userY = int.Parse(place[1].ToString()); // user user.MakeMove(userX, userY); // ticTacToeChecker.CheckGameStatus(ticTacToe); if (ticTacToe.GameStatus == GameStatus.InProgress) { // bot AlphaBetaPruning alphaBetaPruning = new AlphaBetaPruning(ticTacToe.ticTacToeBoard, ticTacToe.Level); Tuple <int, int> xy = alphaBetaPruning.BestMove(); bot.MakeMove(xy.Item1, xy.Item2); // ticTacToeChecker.CheckGameStatus(ticTacToe); if (ticTacToe.GameStatus != GameStatus.InProgress) { ticTacToeChecker.CheckGameStatusAndGivePoint(ticTacToe); } } else { ticTacToeChecker.CheckGameStatusAndGivePoint(ticTacToe); } return(View("Index", ticTacToe)); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { // attempt to sign in user Microsoft.AspNetCore.Identity.SignInResult signInResult = await signInManager.PasswordSignInAsync(Input.NickName, Input.Password, Input.RememberMe, lockoutOnFailure : false); if (signInResult.Succeeded) { logger.LogInformation($"User {Input.NickName} logged in."); return(LocalRedirect(returnUrl)); } // attempt to sign up user var user = new TicTacToeUser { UserName = Input.NickName }; IdentityResult signUpResult = await userManager.CreateAsync(user, Input.Password); if (signUpResult.Succeeded) { logger.LogInformation($"User {Input.NickName} created."); await signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl ?? Url.Content("~/"))); } foreach (IdentityError error in signUpResult.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var userManager = context.OwinContext.GetUserManager <ApplicationUserManager>(); TicTacToeUser 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); }