public async Task <IActionResult> Register([FromBody] RegisterDTO model)
        {
            EnsureDatabaseCreated(_context);
            if (ModelState.IsValid)
            {
                bool duplicate = false;
                var  user      = await _userManager.FindByNameAsync(model.NationalCode);

                if (user != null)
                {
                    duplicate = true;
                    ModelState.AddModelError("NationalCode", "کد ملی در سیستم قبلا ثبت شده است");
                }

                var user1 = _context.Users.Any(x => x.PhoneNumber == model.PhonNumber);

                if (user1)
                {
                    duplicate = true;
                    ModelState.AddModelError("PhoneNumber", "شماره تلفن همراه قبلا ثبت شده است");
                }


                if (!model.PhonNumber.IsValidIranianMobileNumber())
                {
                    duplicate = true;
                    ModelState.AddModelError("PhoneNumber", "شماره تلفن همراه صحیح نمی باشد");
                }

                if (duplicate)
                {
                    return(BadRequest(ModelState));
                }

                user = new ApplicationUser
                {
                    UserName     = model.PhonNumber,
                    PhoneNumber  = model.PhonNumber,
                    PasswordHash = HashClass.BitConverterHasj(model.Password),
                    FullName     = model.FullName,
                    NationalCode = model.NationalCode
                };
                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    return(Ok(true));
                }
                AddErrors(result);
            }

            // If we got this far, something failed.
            return(BadRequest(ModelState));
        }
Esempio n. 2
0
        public async Task <IActionResult> Exchange(OpenIdConnectRequest request)
        {
            Debug.Assert(request.IsTokenRequest(),
                         "The OpenIddict binder for ASP.NET Core MVC is not registered. " +
                         "Make sure services.AddOpenIddict().AddMvcBinders() is correctly called.");

            if (request.IsPasswordGrantType())
            {
                var user = await _userManager.FindByNameAsync(request.Username);

                //foreach (var item in resalt.Errors)
                //{
                //    ModelState.AddModelError("", item.Description);
                //}
                var hasError = false;
                if (user == null)
                {
                    ModelState.AddModelError("NationalCode", "نام کاربری موجود نیست");
                    hasError = true;
                    //return BadRequest(new
                    //{
                    //    //Error = "NationalCode",// OpenIdConnectConstants.Errors.InvalidGrant,
                    //    NationalCode = "نام کاربری موجود نیست"
                    //});
                }
                else
                {
                    var result = HashClass.BitConverterHasj(request.Password) == user.PasswordHash; //  await _signInManager.CheckPasswordSignInAsync(user, HashClass.BitConverterHasj(request.Password), lockoutOnFailure: true);
                    if (!result)                                                                    //result.Succeeded
                    {
                        ModelState.AddModelError("Password", "کلمه عبور اشتباه است");
                        hasError = true;
                        //return BadRequest(new
                        //{
                        //    //Error = "Password",//OpenIdConnectConstants.Errors.InvalidGrant,
                        //    Password = "******"
                        //});
                    }
                }



                if (hasError)
                {
                    return(BadRequest(ModelState));
                }
                // Create a new authentication ticket.
                var ticket = await CreateTicketAsync(request, user);

                ticket.SetAccessTokenLifetime(TimeSpan.FromDays(7));
                //ticket.SetAuthorizationCodeLifetime(TimeSpan.FromMinutes(1));
                //ticket.SetIdentityTokenLifetime(TimeSpan.FromMinutes(30));
                //ticket.SetRefreshTokenLifetime(TimeSpan.FromDays(2));

                var tocken = SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme);

                return(tocken);
            }

            return(BadRequest(new OpenIdConnectResponse
            {
                Error = OpenIdConnectConstants.Errors.UnsupportedGrantType,
                ErrorDescription = "The specified grant type is not supported."
            }));
            //return Ok("");
        }