<commit_before>﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Rinsen.IdentityProvider.Core;
using Rinsen.IdentityProviderWeb.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Rinsen.IdentityProviderWeb.Controllers
{
    public class IdentityController : Controller
    {
        private readonly ILoginService _loginService;

        [HttpGet]
        [AllowAnonymous]
        public IActionResult Login(string returnUrl)
        {

            

            return View(new LoginModel());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _loginService.LoginAsync(model.Email, model.Password, model.RememberMe);

                if (result.Succeeded)
                {
                    return RedirectToTrustedHostOnly(model.ReturnUrl);
                }

                ModelState.AddModelError(string.Empty, "Invalid login attempt.");
            }

            return View(model);
        }

        

        [HttpGet]
        [AllowAnonymous]
        public IActionResult Create()
        {



            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(LoginModel model)
        {


            return View(model);
        }

        [HttpGet]
        public IActionResult Logout()
        {



            return View();
        }

        private IActionResult RedirectToLocalOrTrustedHostOnly(string returnUrl)
        {
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }

            var result = _externalHostValidator.ValidateAsync(returnUrl);

            if (result.Succeeded)
            {
                var uriBuilder = new UriBuilder(returnUrl);
                var query = QueryHelpers.ParseQuery(uriBuilder.Query);
                query["Token"] = result.Token;
                uriBuilder.Query = query.ToString();

                return Redirect(uriBuilder.ToString());
            }

            return RedirectToAction(nameof(HomeController.Index), "Home");
        }
    }
}<commit_msg>Alternative solution<commit_after>﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Rinsen.IdentityProvider.Core;
using Rinsen.IdentityProviderWeb.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Rinsen.IdentityProviderWeb.Controllers
{
    public class IdentityController : Controller
    {
        private readonly ILoginService _loginService;

        [HttpGet]
        [AllowAnonymous]
        public IActionResult Login(string returnUrl)
        {

            

            return View(new LoginModel());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _loginService.LoginAsync(model.Email, model.Password, model.RememberMe);

                if (result.Succeeded)
                {
                    return RedirectToLocalOrTrustedHostOnly(model.ReturnUrl);
                }

                ModelState.AddModelError(string.Empty, "Invalid login attempt.");
            }

            return View(model);
        }

        [HttpGet]
        [AllowAnonymous]
        public IActionResult Create()
        {



            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(LoginModel model)
        {


            return View(model);
        }

        [HttpGet]
        public IActionResult Logout()
        {



            return View();
        }

        private IActionResult RedirectToLocalOrTrustedHostOnly(string returnUrl)
        {
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }

            var result = _externalHostValidator.ValidateAsync(returnUrl);

            if (result.Succeeded)
            {
                // This
                var uriBuilder = new UriBuilder(returnUrl);
                var query = QueryHelpers.ParseQuery(uriBuilder.Query);
                query["Token"] = result.Token;
                uriBuilder.Query = query.ToString();

                // Or this?
                var uri = QueryHelpers.AddQueryString(returnUrl, "Token", result.Token);

                return Redirect(uriBuilder.ToString());
            }

            return RedirectToAction(nameof(HomeController.Index), "Home");
        }
    }
}
{'fraction_non_alphanumeric': 0.08018867924528301, 'fraction_numerical': 0.0, 'mean_word_length': 1.7741751990898749, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '981053', 'n_tokens_mistral': 1261, 'n_tokens_neox': 1202, 'n_words': 233}