예제 #1
0
        /// <summary>
        /// Logins with the specified model asynchronous.
        /// </summary>
        /// <param name="model">The login model.</param>
        /// <returns>A <see cref="Task{TResult}"/> represents the login operation.</returns>
        public async Task <IResponse> LoginAsync(OpenIdLoginModel model)
        {
            var user = await _userManager.FindByLoginAsync(WChatLoginProvider, model.OpenId);

            if (user == null)
            {
                throw new ArgumentException("cannot login with weixin open id.");
            }

            await _signInManager.SignInAsync(user, isPersistent : false);

            var userName = await _userManager.GetUserNameAsync(user);

            var phoneNumber = await _userManager.GetPhoneNumberAsync(user);

            var code = await _userManager.GenerateChangePhoneNumberTokenAsync(user, phoneNumber);

            var response = await RequestTokenAsync(userName, code);

            if (response.IsError)
            {
                _logger.LogError(response.ToString());

                throw new ArgumentNullException(response.Error);
            }

            return(response);
        }
예제 #2
0
        public async Task <IResponse> Login([FromBody] OpenIdLoginModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            return(await _service.LoginAsync(model));
        }
        public virtual ActionResult OpenId(OpenIdLoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            Identifier id;

            if (Identifier.TryParse(model.OpenIdIdentifier, out id))
            {
                try {
                    var openId = new OpenIdRelyingParty();

                    var returnUrl = new Uri(Url.Action("OpenIdCallback", "Authentication",
                                                       RouteParameter.Add(ThenAttribute.ParameterName,
                                                                          ViewBag.Then[ThenAttribute.ParameterName]),
                                                       Request.Url.Scheme), UriKind.Absolute);

                    var request = openId.CreateRequest(id, Realm.AutoDetect, returnUrl);

                    request.AddExtension(new ClaimsRequest {
                        Email    = DemandLevel.Require,
                        FullName = DemandLevel.Require,
                        Nickname = DemandLevel.Require
                    });

                    var fetchRequest = new FetchRequest();
                    fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.FullName);
                    fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.First);
                    fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.Last);
                    fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
                    request.AddExtension(fetchRequest);

                    return(request.RedirectingResponse.AsActionResult());
                } catch (ProtocolException) {
                    ErrorNotification(Resources.Messages_OpenIdConnectionFailure, false);

                    return(View("Login", model));
                }
            }

            ErrorNotification(Resources.Messages_InvalidOpenIdIdentifier, false);

            return(View("Login", model));
        }
        public virtual ActionResult OpenIdCallback()
        {
            var model = new OpenIdLoginModel();

            var openId = new OpenIdRelyingParty();
            var authenticationResponse = openId.GetResponse();

            if (authenticationResponse.Status == AuthenticationStatus.Authenticated)
            {
                var friendlyName = GetFriendlyName(authenticationResponse);

                SetAuthCookie(authenticationResponse.ClaimedIdentifier, true, friendlyName);

                SuccessNotification(Resources.Messages_OpenIdLoginSuccess);

                return(RedirectToThen());
            }

            ErrorNotification(Resources.Messages_OpenIdLoginFailure, false);

            return(View("Login", model));
        }
        public virtual ActionResult Login()
        {
            var model = new OpenIdLoginModel();

            return(View(model));
        }