Esempio n. 1
0
        public async Task <ActionResult> Register(CustomRegisterUser registerUser)
        {
            var user = new MyIdentityUser
            {
                UserName       = registerUser.Name,
                Email          = registerUser.Email,
                EmailConfirmed = true
            };

            var result = await _userManager.CreateAsync(user, registerUser.Password);


            if (result.Succeeded)
            {
                await _userManager.AddToRoleAsync(user, Roles.BASIC);

                await _userManager.AddClaimAsync(user, new Claim(Claims.Sample, ClaimsValues.READ));

                await _userManager.AddClaimAsync(user, new Claim(Claims.Sample, ClaimsValues.WRITE));

                return(Ok(createToken(user.UserName)));
            }
            else
            {
                var error = new BadRequestError();
                foreach (var item in result.Errors)
                {
                    error.AddMessage(item.Description);
                }
                var json = ErrorFormat.SerializeError(ModelState, error);
                return(BadRequest(json));
            }
        }
Esempio n. 2
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            // When you have specifics configurations (see inside this method)
            services.AddCustomIdentityConfiguration(Configuration);

            services.AddDistributedRedisCache(options =>
            {
                options.Configuration =
                    Configuration.GetConnectionString("ConexaoRedis");
                options.InstanceName = "APICotacoes-";
            });

            services.AddMemoryCache();

            services.Configure <ApiBehaviorOptions>(options =>
            {
                options.InvalidModelStateResponseFactory = context =>
                {
                    var errors   = context.ModelState.Values.SelectMany(e => e.Errors);
                    var apiError = new BadRequestError();

                    foreach (var error in errors)
                    {
                        apiError.AddMessage(error.ErrorMessage);
                    }

                    string json = JsonConvert.SerializeObject(apiError, Formatting.Indented);
                    var result  = new BadRequestObjectResult(json);
                    result.ContentTypes.Clear();
                    return(result);
                };
            });


            services.AddAspNetUserConfiguration();

            services.AddSwaggerConfiguration();

            services.AddDependencyInjectionConfiguration(Configuration);
        }
Esempio n. 3
0
        public async Task <ActionResult> ResetPassword(ResetPasswordModel model, [FromServices] IMemoryCache cache)
        {
            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                return(BadRequest(ErrorFormat.SerializeError(new BadRequestError("Invalid reset credentiais"))));
            }

            var result = await _userManager.ResetPasswordAsync(user, model.Token, model.Password);

            if (!result.Succeeded)
            {
                var error = new BadRequestError();
                foreach (var item in result.Errors)
                {
                    error.AddMessage(item.Description);
                }
                var json = ErrorFormat.SerializeError(ModelState, error);
                return(BadRequest(json));
            }

            return(Ok("Ok"));
        }