public async Task<IActionResult> Register(ViewModels.RegisterViewModel Model)
 {
     if(ModelState.IsValid)
     {
         var user = new Models.ApplicationUser()
         {
             Email = Model.EmailAddress,
             UserName = Model.EmailAddress,
             FirstName = Model.FirstName,
             LastName = Model.LastName,
             AccountEnabled = true
         };
         var createResult = await _UserManager.CreateAsync(user, Model.Password);
     }
     return View();
 }
Esempio n. 2
0
        public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
        {
            returnUrl = String.IsNullOrEmpty(returnUrl) ? "/Auth/Success" : returnUrl;
            var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
            if (loginInfo == null)
            {
                return RedirectToAction("Login");
            }

            // Sign in the user with this external login provider if the user already has a login
            var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: true);
            switch (result)
            {
                case SignInStatus.Success:
                    Models.ApplicationUser theuser = UserManager.FindByName(loginInfo.Login.ProviderKey);
                    string oldRefreshToken = theuser.RefreshToken;

                    theuser.AccessToken = loginInfo.ExternalIdentity.FindFirst("urn:reddit:accesstoken").Value;
                    theuser.RefreshToken = loginInfo.ExternalIdentity.FindFirst("urn:reddit:refresh").Value;
                    theuser.TokenExpires = DateTime.Parse(loginInfo.ExternalIdentity.FindFirst("urn:reddit:accessexpires").Value);

                    string[] scope = loginInfo.ExternalIdentity.FindFirst("urn:reddit:scope").Value.ToLower().Split(',');


                    if (scope.Contains("wikiread"))
                    {
                        if (!theuser.HasWikiRead) theuser.HasWikiRead = true;
                    }
                    else if (theuser.HasWikiRead) theuser.HasWikiRead = false;

                    if (scope.Contains("read"))
                    {
                        if (!theuser.HasRead)  theuser.HasRead = true;
                    }
                    else if (theuser.HasRead) theuser.HasRead = false;

                    UserManager.Update(theuser);

                    SignInManager.SignIn(theuser, isPersistent: true, rememberBrowser: false);
					try {
						Utilities.AuthUtils.RevokeRefreshToken( oldRefreshToken );
					}
					catch {
						//ignore the inability to revoke the token. It doesn't matter;
					}

                    return new RedirectResult(returnUrl);
                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.RequiresVerification:
                    return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
                case SignInStatus.Failure:
                default:
                    // If the user does not have an account, then prompt the user to create an account
                    string accessToken = loginInfo.ExternalIdentity.FindFirst("urn:reddit:accesstoken").Value;
                    var user = new Models.ApplicationUser() { UserName = loginInfo.Login.ProviderKey, RefreshToken = loginInfo.ExternalIdentity.FindFirst("urn:reddit:refresh").Value, AccessToken = loginInfo.ExternalIdentity.FindFirst("urn:reddit:accesstoken").Value, TokenExpires = DateTime.UtcNow.AddMinutes(50), LastUpdatedRoles = DateTime.UtcNow };
                    await Utilities.AuthUtils.UpdateModeratedSubreddits(user, UserManager);
                    var createuser = await UserManager.CreateAsync(user);
                    if (createuser.Succeeded)
                    {
                        var addLogin = await UserManager.AddLoginAsync(user.Id, loginInfo.Login);
                        if (addLogin.Succeeded)
                        {
                            await SignInManager.SignInAsync(user, isPersistent: true, rememberBrowser: false);
                            return new RedirectResult(returnUrl);
                        }
                    }
                    return View("Error");
            }


        }