示例#1
0
 public async Task <IActionResult> Create(SchuelerModel neuerschueler)
 {
     try
     {
         if (ModelState.IsValid)
         {
             await SchuelerProcessor.CreateSchuelerAsync(neuerschueler.Checkpersonnumber.ToString(), neuerschueler.NName, neuerschueler.VName, neuerschueler.Geschlecht, neuerschueler.EmailAdresse, neuerschueler.Schuljahr, ConnectionString);
         }
         return(RedirectToAction("Index"));
     }
     catch (Exception ex)
     {
         return(View(ex.Message));
     }
 }
        public async Task <ActionResult <string> > PostSchueler(SchuelerModel schueler)
        {
            try
            {
                await Task.Delay(10);

                await SchuelerProcessor.CreateSchuelerAsync(
                    schueler.Checkpersonnumber.ToString(),
                    schueler.NName,
                    schueler.VName,
                    schueler.Geschlecht,
                    schueler.EmailAdresse,
                    schueler.Schuljahr,
                    ConnectionString);

                return("schueler_created");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
示例#3
0
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information during confirmation.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            if (ModelState.IsValid)
            {
                var user = new IdentityUser {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        // If they exist, add claims to the user for:
                        //    Given (first) name
                        //    Locale
                        //    Picture
                        if (info.Principal.HasClaim(c => c.Type == ClaimTypes.GivenName))
                        {
                            string firstname = "";
                            string lastname  = "";
                            string email     = "";
                            string gender    = "";
                            string userid    = "";
                            bool   IsTeacher = false;
                            var    claims    = await _userManager.GetClaimsAsync(user);

                            var x = await _userManager.AddClaimAsync(user,
                                                                     info.Principal.FindFirst(ClaimTypes.GivenName));

                            x = await _userManager.AddClaimAsync(user,
                                                                 info.Principal.FindFirst(ClaimTypes.Email));

                            x = await _userManager.AddClaimAsync(user,
                                                                 info.Principal.FindFirst(ClaimTypes.Surname));

                            x = await _userManager.AddClaimAsync(user,
                                                                 info.Principal.FindFirst(ClaimTypes.NameIdentifier));

                            foreach (var c in claims)
                            {
                                if (c.Type == ClaimTypes.NameIdentifier)
                                {
                                    userid = c.Value;
                                }
                                if (c.Type == ClaimTypes.GivenName)
                                {
                                    firstname = c.Value;
                                }
                                if (c.Type == ClaimTypes.Surname)
                                {
                                    lastname = c.Value;
                                }
                                if (c.Type == ClaimTypes.Email)
                                {
                                    email = c.Value;
                                }
                                //if (c.Type == ClaimTypes.Role && c.Value == "Teacher")
                                //    IsTeacher = true;
                            }
                            //var peopleinfo = await GetGoogleData.GetPeopleInfo(credential,userid);
                            var schueler = await SchuelerProcessor.ExistsSchuelerByIdAsync(userid, ConnectionString);

                            if (!schueler)
                            {
                                var breakpoint = 0;
                                await SchuelerProcessor.CreateSchuelerAsync(userid, lastname, firstname, "M", email, 12, ConnectionString);
                            }
                        }

                        // Include the access token in the properties
                        var props = new AuthenticationProperties();
                        props.StoreTokens(info.AuthenticationTokens);
                        props.IsPersistent = true;
                        //TokenResponse tok = new TokenResponse
                        //{
                        //    AccessToken = props.GetTokenValue("accesstoken"),
                        //    TokenType = "Bearer"
                        //};
                        //var credential = GoogleProviderHelper.CreateUserCredential(info.Principal);
                        //var u = await GetGoogleData.GetClassroomUserProfile(credential);
                        //if (u.VerifiedTeacher.Value)
                        //x = await _userManager.AddClaimAsync(user, new Claim(ClaimTypes.Role, "Teacher"));


                        await _signInManager.SignInAsync(user, isPersistent : false);

                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
                        //////
                        var dd     = info.Principal;
                        var userId = await _userManager.GetUserIdAsync(user);

                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                        var callbackUrl = Url.Page(
                            "/Account/ConfirmEmail",
                            pageHandler: null,
                            values: new { area = "Identity", userId = userId, code = code },
                            protocol: Request.Scheme);

                        await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                          $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            LoginProvider = info.LoginProvider;
            ReturnUrl     = returnUrl;
            return(Page());
        }