| | 35 | {{{ |
| | 36 | var googleId = System.Configuration.ConfigurationManager.AppSettings["GoogleClientId"]; |
| | 37 | var googleSecret = System.Configuration.ConfigurationManager.AppSettings["GoogleClientSecret"]; |
| | 38 | if (!string.IsNullOrWhiteSpace(googleId) && !string.IsNullOrWhiteSpace(googleSecret)) |
| | 39 | { |
| | 40 | app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions |
| | 41 | { |
| | 42 | ClientId = googleId, |
| | 43 | ClientSecret = googleSecret, |
| | 44 | CallbackPath = new PathString("/signin-google"), |
| | 45 | CookieManager = cookieManager |
| | 46 | }); |
| | 47 | } |
| | 48 | }}} |
| | 49 | |
| | 50 | {{{ |
| | 51 | // POST: /Account/ExternalLogin |
| | 52 | [HttpPost] |
| | 53 | [AllowAnonymous] |
| | 54 | [ValidateAntiForgeryToken] |
| | 55 | public ActionResult ExternalLogin(string provider, string returnUrl) |
| | 56 | { |
| | 57 | // Request a redirect to the external login provider |
| | 58 | var redirectUrl = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }); |
| | 59 | return new ChallengeResult(provider, redirectUrl); |
| | 60 | } |
| | 61 | |
| | 62 | |
| | 63 | // POST: /Account/ExternalLoginConfirmation |
| | 64 | [HttpPost] |
| | 65 | [AllowAnonymous] |
| | 66 | [ValidateAntiForgeryToken] |
| | 67 | public async Task<ActionResult> ExternalLoginConfirmation(WebApplication1.Models.ExternalLoginConfirmationViewModel model, string returnUrl) |
| | 68 | { |
| | 69 | if (User.Identity.IsAuthenticated) |
| | 70 | { |
| | 71 | return RedirectToAction("Index", "Manage"); |
| | 72 | } |
| | 73 | |
| | 74 | if (!ModelState.IsValid) |
| | 75 | { |
| | 76 | return View(model); |
| | 77 | } |
| | 78 | |
| | 79 | // Obtain external login info again |
| | 80 | var info = await AuthenticationManager.GetExternalLoginInfoAsync(); |
| | 81 | if (info == null) |
| | 82 | { |
| | 83 | ModelState.AddModelError("", "Не можам да ги вчитам информации за надворешната најава."); |
| | 84 | return View(model); |
| | 85 | } |
| | 86 | |
| | 87 | ApplicationUser user; |
| | 88 | if (model.UserType == "Student") |
| | 89 | { |
| | 90 | user = new Student |
| | 91 | { |
| | 92 | UserName = model.Email, |
| | 93 | Email = model.Email, |
| | 94 | Name = model.Name, |
| | 95 | Surname = model.Surname, |
| | 96 | Biography = model.Biography, |
| | 97 | Index = model.Index, |
| | 98 | Major = model.Major, |
| | 99 | Cycle = model.Cycle, |
| | 100 | Semester = model.Semester |
| | 101 | }; |
| | 102 | } |
| | 103 | else // Mentor |
| | 104 | { |
| | 105 | user = new Mentor |
| | 106 | { |
| | 107 | UserName = model.Email, |
| | 108 | Email = model.Email, |
| | 109 | Name = model.Name, |
| | 110 | Surname = model.Surname, |
| | 111 | Biography = model.Biography, |
| | 112 | Timeslots = model.Timeslots, |
| | 113 | TypesOfProject = model.TypesOfProject, |
| | 114 | Available = model.Available, |
| | 115 | ImageURL = null |
| | 116 | }; |
| | 117 | } |
| | 118 | |
| | 119 | // Create user |
| | 120 | var result = await UserManager.CreateAsync(user); |
| | 121 | if (!result.Succeeded) |
| | 122 | { |
| | 123 | AddErrors(result); |
| | 124 | return View(model); |
| | 125 | } |
| | 126 | |
| | 127 | // Add the external login |
| | 128 | result = await UserManager.AddLoginAsync(user.Id, info.Login); |
| | 129 | if (!result.Succeeded) |
| | 130 | { |
| | 131 | AddErrors(result); |
| | 132 | return View(model); |
| | 133 | } |
| | 134 | |
| | 135 | if (model.UserType == "Student") |
| | 136 | { |
| | 137 | var student = db.Users |
| | 138 | .OfType<Student>() |
| | 139 | .Include(s => s.Subjects) |
| | 140 | .Include(s => s.Topics) |
| | 141 | .SingleOrDefault(u => u.Id == user.Id); |
| | 142 | |
| | 143 | if (student != null) |
| | 144 | { |
| | 145 | // clear any placeholder (should be empty for a brand new user) and add incoming items |
| | 146 | student.Subjects.Clear(); |
| | 147 | student.Topics.Clear(); |
| | 148 | |
| | 149 | foreach (var subjName in model.Subjects ?? Enumerable.Empty<string>()) |
| | 150 | student.Subjects.Add(new Subject { Name = subjName, UserId = student.Id }); |
| | 151 | |
| | 152 | foreach (var topicName in model.Topics ?? Enumerable.Empty<string>()) |
| | 153 | student.Topics.Add(new Topic { Name = topicName, UserId = student.Id }); |
| | 154 | |
| | 155 | db.SaveChanges(); |
| | 156 | } |
| | 157 | } |
| | 158 | else // Mentor |
| | 159 | { |
| | 160 | var mentor = db.Users |
| | 161 | .OfType<Mentor>() |
| | 162 | .Include(m => m.Subjects) |
| | 163 | .Include(m => m.Topics) |
| | 164 | .SingleOrDefault(u => u.Id == user.Id); |
| | 165 | |
| | 166 | if (mentor != null) |
| | 167 | { |
| | 168 | mentor.Subjects.Clear(); |
| | 169 | mentor.Topics.Clear(); |
| | 170 | |
| | 171 | foreach (var subjName in model.Subjects ?? Enumerable.Empty<string>()) |
| | 172 | mentor.Subjects.Add(new Subject { Name = subjName, UserId = mentor.Id }); |
| | 173 | |
| | 174 | foreach (var topicName in model.Topics ?? Enumerable.Empty<string>()) |
| | 175 | mentor.Topics.Add(new Topic { Name = topicName, UserId = mentor.Id }); |
| | 176 | |
| | 177 | db.SaveChanges(); |
| | 178 | } |
| | 179 | } |
| | 180 | |
| | 181 | |
| | 182 | [HttpPost] |
| | 183 | [ValidateAntiForgeryToken] |
| | 184 | public ActionResult LinkLogin(string provider) |
| | 185 | { |
| | 186 | // Request redirect to external login provider to link to current user |
| | 187 | return new ChallengeResult(provider, Url.Action("LinkLoginCallback", "Account"), User.Identity.GetUserId()); |
| | 188 | } |
| | 189 | |
| | 190 | [AllowAnonymous] |
| | 191 | public async Task<ActionResult> LinkLoginCallback() |
| | 192 | { |
| | 193 | var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(XsrfKey, User.Identity.GetUserId()); |
| | 194 | if (loginInfo == null) |
| | 195 | { |
| | 196 | return RedirectToAction("Manage", new { Message = ManageMessageId.Error }); |
| | 197 | } |
| | 198 | var result = await UserManager.AddLoginAsync(User.Identity.GetUserId(), loginInfo.Login); |
| | 199 | if (result.Succeeded) |
| | 200 | { |
| | 201 | return RedirectToAction("Manage", new { Message = ManageMessageId.AddLoginSuccess }); |
| | 202 | } |
| | 203 | return RedirectToAction("Manage", new { Message = ManageMessageId.Error }); |
| | 204 | } |
| | 205 | |
| | 206 | |
| | 207 | }}} |
| | 208 | |