Ejemplo n.º 1
0
        public USER CreateFromExternalLoginModel(LoginExternalViewModel model)
        {
            EMAIL     = model.Email;
            LASTNAME  = model.LastName;
            FIRSTNAME = model.FirstName;

            return(this);
        }
Ejemplo n.º 2
0
        public async Task <JsonResult> LoginExternal(LoginExternalViewModel model)
        {
            if (string.IsNullOrEmpty(model.UserName) || string.IsNullOrEmpty(model.Password))
            {
                return(Json(new
                {
                    Result = "无效的登录尝试。",
                    Token = "",
                    Success = false
                }));
            }
            var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:
                var token = await GetAccessToken(model);

                var roles =
                    await(await _httpClient.GetAsync("/api/UserRoles?userName="******"System.String"));
                await _httpClient.GetAsync("/api/UserRecord?userName="******"&token=" + token
                                           + "&roles=" + roles);

                return(Json(new
                {
                    Result = "Success",
                    Token = token,
                    Success = true
                }, JsonRequestBehavior.AllowGet));

            case SignInStatus.LockedOut:
                return(Json(new
                {
                    Result = "用戶狀態異常",
                    Token = "",
                    Success = false
                }, JsonRequestBehavior.AllowGet));;

            case SignInStatus.RequiresVerification:
                return(Json(new
                {
                    Result = "目前版本不支持雙因素登錄",
                    Token = "",
                    Success = false
                }, JsonRequestBehavior.AllowGet));

            case SignInStatus.Failure:
            default:
                return(Json(new
                {
                    Result = "无效的登录尝试。",
                    Token = "",
                    Success = false
                }));
            }
        }
        public ActionResult LoginOrRegisterExternal(LoginExternalViewModel model)
        {
            if (ModelState.IsValid)
            {
                USER_Service userService = new USER_Service();
                USER         user        = userService.FindByEmail(model.Email);

                if (user == null)
                {
                    user = new USER().CreateFromExternalLoginModel(model);

                    //Génération du username
                    var generatedUsername = user.FIRSTNAME.ToLower() + user.LASTNAME.ToLower();
                    var usernameAlreadyExistsWithSameBaseCount =
                        userService.FindAllByUsernameStartWith(generatedUsername).Count();
                    if (usernameAlreadyExistsWithSameBaseCount > 0)
                    {
                        generatedUsername += usernameAlreadyExistsWithSameBaseCount;
                    }
                    user.USERNAME = generatedUsername;

                    //Ajout en BDD
                    bool isRegistered = userService.AddOrUpdateUser(user);

                    if (isRegistered)
                    {
                        SetCurrentUser(user.USERNAME, user.PK_ID_USER);
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    switch (model.ProviderType)
                    {
                    case LoginExternalViewModel.Provider.Google:
                        user.GOOGLEID = model.ProviderUserId;
                        break;

                    case LoginExternalViewModel.Provider.Facebook:
                        user.FACEBOOKID = model.ProviderUserId;
                        break;
                    }
                    userService.AddOrUpdateUser(user);

                    SetCurrentUser(user.USERNAME, user.PK_ID_USER);
                    return(RedirectToAction("Index", "Home"));
                }
            }

            ModelState.AddModelError("", UiStrings.login_error_auth);
            return(View("Login", new LoginViewModel()));
        }
Ejemplo n.º 4
0
        public async Task <JsonResult> LoginExternal(LoginExternalViewModel model)
        {
            if (string.IsNullOrEmpty(model.UserName) || string.IsNullOrEmpty(model.Password))
            {
                return(Json(new
                {
                    Result = "无效的登录尝试。",
                    Token = "",
                    Success = false
                }));
            }
            var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:
                var token = await GetAccessToken(model);

                return(Json(new
                {
                    Result = "Success",
                    Token = token,
                    Success = true
                }, JsonRequestBehavior.AllowGet));

            case SignInStatus.LockedOut:
                return(Json(new
                {
                    Result = "用戶狀態異常",
                    Token = "",
                    Success = false
                }, JsonRequestBehavior.AllowGet));;

            case SignInStatus.RequiresVerification:
                return(Json(new
                {
                    Result = "目前版本不支持雙因素登錄",
                    Token = "",
                    Success = false
                }, JsonRequestBehavior.AllowGet));

            case SignInStatus.Failure:
            default:
                return(Json(new
                {
                    Result = "无效的登录尝试。",
                    Token = "",
                    Success = false
                }));
            }
        }
Ejemplo n.º 5
0
        public async Task <JsonResult> AuthorizationRequest(AuthorizationRequest request)
        {
            var modal = new LoginExternalViewModel
            {
                UserName = request.UserName,
                Password = request.Password,
                PeerUrl  = request.PeerUrl
            };

            if (!string.IsNullOrEmpty(modal.UserName) && !string.IsNullOrEmpty(modal.Password) &&
                !string.IsNullOrEmpty(modal.PeerUrl))
            {
                var token = await GetAccessToken(modal);

                _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                var result =
                    await(await _httpClient.GetAsync(request.RequestUrl)).Content.ReadAsAsync(
                        Type.GetType(request.ReturnType));
                return(Json(result, JsonRequestBehavior.AllowGet));
            }

            return(Json(null, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 6
0
        private async Task <string> GetAccessToken(LoginExternalViewModel model)
        {
            var parameters = new Dictionary <string, string>
            {
                { "grant_type", "password" },
                { "username", model.UserName },
                { "password", model.Password }
            };

            _httpClient.BaseAddress = new Uri(model.PeerUrl);

            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "Basic",
                Convert.ToBase64String(Encoding.ASCII.GetBytes(model.UserName + ":" + model.Password))
                );

            var response = await _httpClient.PostAsync("/token", new FormUrlEncodedContent(parameters));

            var responseValue = await response.Content.ReadAsStringAsync();

            return(response.StatusCode == System.Net.HttpStatusCode.OK
                ? JObject.Parse(responseValue)["access_token"].Value <string>()
                : string.Empty);
        }