Esempio n. 1
0
        public JsonResult GetToken(string code, string appId, string appSecretKey, 
            string responseType, string grantType)
        {
            if (string.IsNullOrEmpty(code))
                throw new ArgumentNullException("code");

            if (string.IsNullOrEmpty(appId))
                throw new ArgumentNullException("appId");

            if (string.IsNullOrEmpty(appSecretKey))
                throw new ArgumentNullException("appSecretKey");

            if (string.IsNullOrEmpty(responseType))
                throw new ArgumentNullException("responseType");

            if (responseType != "token")
                throw new ArgumentException("Неправильное значение responseType");

            if (string.IsNullOrEmpty(grantType))
                throw new ArgumentNullException("grantType");

            if (grantType != "code")
                throw new ArgumentException("Неправильное значение grantType");

            var app = appRepo.GetAll().FirstOrDefault(e => e.AppId == appId);
            if (app == null)
                throw new Exception("Нет приложения с таким appId");

            if (app.AppSecretKey != appSecretKey)
                throw new ArgumentException("Неверное значение appSecretKey");

            var authCode = codes.FirstOrDefault(e => e.Key == code);
            if (authCode.Key == null)
                throw new ArgumentException("Неверное значение code");

            var user = new UsersRepository().GetAll()
                .FirstOrDefault(e => e.Id == authCode.Value);

            var token = SessionHelper.GenerateToken(code, user, app);

            tokensRepo.Save(token);
            codes.Remove(authCode.Key);

            return Json(new
            {
                token.Token1,
                tokenType = "bearer",
                ExpiresIn = SessionHelper.ExpiresInSeconds,
                token.RefreshToken
            });
        }
Esempio n. 2
0
        private User Auth(string login, string password)
        {
            var usersRepo = new UsersRepository();
            var hash = UserDto.GetHashForString(password);

            return usersRepo.GetAll().FirstOrDefault(e =>
                e.Login == login
                && e.PasswordHash == hash);
        }