Esempio n. 1
0
        public async Task <IActionResult> Authorize(AuthorizeModel model)
        {
            var user_id = HttpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;

            var consent = await _authUnitOfWork.ConsentRepository.GetUserConsentByClientId(model.client_id, user_id);

            if (consent == null)
            {
                consent = consent ?? new Consent <User>()
                {
                    Client_Id = model.client_id,
                    User_Id   = user_id,
                    Scope     = model.scope
                };
                _authUnitOfWork.ConsentRepository.AddConsent(consent);
                await _authUnitOfWork.SaveAsync();
            }

            var existingcode = await _authorizationCodeRepository.GetAuthorizationCodeByUserId(model.client_id, user_id);

            if (existingcode != null)
            {
                //_authorizationCodeRepository.RemoveRange(new List<AuthorizationCode>() { existingcode });
                existingcode.Expired = true;
            }

            var hex = RandomStringGenerator.GenerateHex(16);
            //var bytes = new byte[16];
            //new Random().NextBytes(bytes);
            //string hex = BitConverter.ToString(bytes).Replace("-", string.Empty);

            var authCode = new AuthorizationCode <User>(hex, consent, DateTime.Now.AddMinutes(5));

            _authorizationCodeRepository.AddAuthorizationCode(authCode);


            string authorization_code = authCode.Code;

            var redirection_path = model.redirect_uri + "?code=" + authorization_code;

            if (!string.IsNullOrEmpty(model.state))
            {
                redirection_path += "&state=" + model.state;
            }

            return(Redirect(redirection_path));
        }
Esempio n. 2
0
        public async Task <IActionResult> RevokeAccess([FromForm] string Client_Id)
        {
            var user_id = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
            var consent = await _authUnitOfWork.ConsentRepository.GetUserConsentByClientId(Client_Id, user_id);

            if (consent != null)
            {
                _authUnitOfWork.ConsentRepository.DeleteConsent(consent);
                await _authUnitOfWork.SaveAsync();
            }

            return(RedirectToAction("Index"));
        }
Esempio n. 3
0
        public async Task <IActionResult> AddClient(AddClientModel client)
        {
            var user_id     = User.Claims.FirstOrDefault(C => C.Type == ClaimTypes.NameIdentifier).Value;
            var addedClient = new Client()
            {
                Client_Id     = RandomStringGenerator.GenerateHex(16),
                Client_Secret = RandomStringGenerator.GenerateHex(32),
                Developer_Id  = user_id,
                Redirect_Uri  = client.Redirect_Uri,
                Client_Name   = client.Client_Name
            };

            _authUnitOfWork.ClientRepository.AddClient(addedClient);
            await _authUnitOfWork.SaveAsync();

            return(RedirectToAction("Index"));
        }