コード例 #1
0
    public ActionResult <ShopkeeperSignInResponse> PostSignIn(ShopkeeperSignInRequest request)
    {
        var shopkeeper = _context
                         .Shopkeeper
                         .Where(a => a.Email == request.Email)
                         .FirstOrDefault();

        if (shopkeeper == null)
        {
            return(NotFound());
        }

        if (!Hash.Validate(request.Password, shopkeeper.Password))
        {
            return(NotFound());
        }

        return(Ok(new ShopkeeperSignInResponse()
        {
            Id = shopkeeper.Id,
            Email = shopkeeper.Email,
            Name = shopkeeper.Name,
            IdStore = shopkeeper.IdStore,
            Token = JwtSecurityTokenHelper.BuildToken(configuration["Jwt:Key"], shopkeeper),
        }));
    }
コード例 #2
0
        public async Task <TokenResponse> CreateToken([FromBody] TokenRequestDto model)
        {
            // Validate user credentials and return token
            IActionResult response = Unauthorized();

            if (model == null)
            {
                throw new Exception("request details required");
            }

            if (string.IsNullOrEmpty(model.Username) || string.IsNullOrEmpty(model.Username))
            {
                throw new Exception("Incomplete credentials");
            }

            var result = await _identityService.GetUserAccountAsync(model);

            if (result.Status == AuthStatus.Authenticated)
            {
                // return Json(JwtSecurityTokenHelper.CreateToken(Configuration, result.UserDetails));
                return(JwtSecurityTokenHelper.CreateToken(Configuration, result.UserDetails));

                // return await ApiResponseResult(JwtSecurityTokenHelper.CreateToken(Configuration, result.UserDetails), "Token created");
            }

            return(new TokenResponse
            {
                Token = "",
                Status = System.Net.HttpStatusCode.OK,
                UserDetails = result.UserDetails
            });
        }
コード例 #3
0
    public async Task <ActionResult <ClientSignUpResponse> > PostSignUp(ClientSignUpRequest request)
    {
        if (string.IsNullOrWhiteSpace(request.Name) ||
            string.IsNullOrWhiteSpace(request.Password))
        {
            return(BadRequest("Required parameters"));
        }

        if (ClientExists(request.Email))
        {
            return(Conflict());
        }

        var client = new Client()
        {
            Name     = request.Name,
            Password = Hash.Create(request.Password),
            Email    = request.Email,
        };

        _context.Client.Add(client);
        await _context.SaveChangesAsync();

        return(CreatedAtAction("GetClient", new { id = client.Id }, new ClientSignUpResponse()
        {
            Id = client.Id,
            Email = request.Email,
            Name = request.Name,
            Token = JwtSecurityTokenHelper.BuildToken(configuration["Jwt:Key"], client),
        }));
    }
コード例 #4
0
    public async Task <ActionResult <ShopkeeperSignUpResponse> > PostSignUp(ShopkeeperSignUpRequest request)
    {
        if (string.IsNullOrWhiteSpace(request.Email) ||
            string.IsNullOrWhiteSpace(request.Password))
        {
            return(BadRequest("Required parameters"));
        }

        if (ShopkeeperExists(request.Email))
        {
            return(Conflict());
        }

        request.Password = Hash.Create(request.Password);

        var shopkeeper = new Shopkeeper()
        {
            Email    = request.Email,
            Password = request.Password,
        };

        _context.Shopkeeper.Add(shopkeeper);
        await _context.SaveChangesAsync();

        return(CreatedAtAction("GetShopkeeper", new { id = shopkeeper.Id }, new ShopkeeperSignInResponse()
        {
            Id = shopkeeper.Id,
            Email = shopkeeper.Email,
            Name = shopkeeper.Name,
            Token = JwtSecurityTokenHelper.BuildToken(configuration["Jwt:Key"], shopkeeper),
        }));
    }
コード例 #5
0
        public IActionResult Authenticate()
        {
            //for demo simplicity, use get and no args
            //todo: authenticate validation
            var success = _authService.Validate("test", "test");

            if (!success)
            {
                return(BadRequest(new { message = "Username or password is incorrect" }));
            }

            //todo: get more client claim infos
            var generateTokenArgs = new GenerateTokenArgs();

            generateTokenArgs.Id     = _appSettings.Id;
            generateTokenArgs.Secret = _appSettings.Secret;

            //On successful authentication the Authenticate method generates a JWT(JSON Web Token)
            //using the JwtSecurityTokenHandler class that generates a token that is digitally signed using a secret key stored in appsettings.json.
            //The JWT token is returned to the client application which then must include it in the HTTP Authorization header of subsequent web api requests for authentication.
            var jwtSecurityTokenHelper = new JwtSecurityTokenHelper();
            var token = jwtSecurityTokenHelper.GenerateToken(generateTokenArgs);

            return(Ok(token));
        }
コード例 #6
0
 public OAuthController(AuthUnitOfWork <User> authUnitOfWork,
                        //IClientRepository clientRepository, IConsentRepository<User> consentRepository,
                        IAuthorizationCodeRepository <User> authorizationCodeRepository, JwtSecurityTokenHelper tokenHelper)
 {
     _authUnitOfWork = authUnitOfWork;
     //_clientRepository = clientRepository;
     //_consentRepository = consentRepository;
     _authorizationCodeRepository = authorizationCodeRepository;
     _tokenHelper = tokenHelper;
 }
コード例 #7
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var tokenParams = JwtSecurityTokenHelper.GetTokenParameters(Configuration);


            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(jwtconfig =>
            {
                jwtconfig.TokenValidationParameters = tokenParams;
            });

            _ActivateCORS(services);

            services.AddApplicationInsightsTelemetry();


            services.AddDbContext <PaymentAppContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("PaymentApp"), b => b.MigrationsAssembly("PaymentApp.Api"));
            });


            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info()
                {
                    Title = "PaymentApp API", Version = "v1"
                });
            });


            // Setup MVC and Authorization
            _SetupMVC(services);

            // Dependency injection setup

            var autofacBuilder = new ContainerBuilder();

            autofacBuilder.Populate(services);

            autofacBuilder.RegisterModule <DefaultModule>();

            var provider = autofacBuilder.Build();

            return(new AutofacServiceProvider(provider));
        }
コード例 #8
0
    public ActionResult <ClientSignInResponse> PostSignIn(ClientSignInRequest request)
    {
        var client = _context.Client
                     .Where(a => a.Email == request.Email)
                     .Single();

        if (client == null)
        {
            return(NotFound());
        }

        if (!Hash.Validate(request.Password, client.Password))
        {
            return(NotFound());
        }

        return(Ok(new ClientSignInResponse()
        {
            Id = client.Id,
            Email = client.Email,
            Name = client.Name,
            Token = JwtSecurityTokenHelper.BuildToken(configuration["Jwt:Key"], client),
        }));
    }
コード例 #9
0
 public UserController(JwtSecurityTokenHelper tokenHelper, IUserRepository userRepository, OAuthContext context)
 {
     _tokenHelper    = tokenHelper;
     _userRepository = userRepository;
     _context        = context;
 }
コード例 #10
0
ファイル: AuthController.cs プロジェクト: zxbe/OAuthServer
 public AuthController(JwtSecurityTokenHelper tokenHelper)
 {
     _tokenHelper = tokenHelper;
 }