Exemplo n.º 1
0
        public async Task <ActionResult> VerifyPhoneNumber(VerifyPhoneNumberViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var result =
                await
                _userManager.ChangePhoneNumberAsync(Guid.Parse(User.Identity.GetUserId()), model.PhoneNumber,
                                                    model.Code);

            if (result.Succeeded)
            {
                var user = await _userManager.FindByIdAsync(Guid.Parse(User.Identity.GetUserId()));

                if (user != null)
                {
                    await SignInAsync(user, isPersistent : false);
                }
                return(RedirectToAction("Index", new { Message = ManageMessageId.AddPhoneSuccess }));
            }
            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "Failed to verify phone");
            return(View(model));
        }
Exemplo n.º 2
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
            var data = await context.Request.ReadFormAsync();

            string code         = string.Empty;
            string mobilenumber = string.Empty;

            foreach (KeyValuePair <string, string[]> item in data)
            {
                if (item.Key.Equals("mobileNumber", StringComparison.InvariantCultureIgnoreCase))
                {
                    mobilenumber = item.Value[0];
                }
                if (item.Key.Equals("code", StringComparison.InvariantCultureIgnoreCase))
                {
                    code = item.Value[0];
                }
            }
            ApplicationUser user = null;

            if (!string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(mobilenumber))
            {
                user = await _userManager.FindByPhoneNumberAsync(mobilenumber);

                if (user == null)
                {
                    context.SetError("invalid_grant", "The user name or password is incorrect.");
                    return;
                }
                var result = await _userManager.ChangePhoneNumberAsync(user.Id, mobilenumber, code);

                if (!result.Succeeded)
                {
                    context.SetError("invalid_grant", "The phone number or confirmation code is not valid");
                    return;
                }
            }
            else
            {
                user = await _userManager.FindAsync(context.UserName, context.Password);
            }


            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }

            ClaimsIdentity oAuthIdentity =
                await _userManager.GenerateUserIdentityAsync(user, OAuthDefaults.AuthenticationType);

            ClaimsIdentity cookiesIdentity = await _userManager.GenerateUserIdentityAsync(user,
                                                                                          CookieAuthenticationDefaults.AuthenticationType);

            AuthenticationProperties properties = CreateProperties(user.UserName);
            AuthenticationTicket     ticket     = new AuthenticationTicket(oAuthIdentity, properties);

            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }