Beispiel #1
0
        public ApiToken Token(string code, string provider)
        {
            var      externalUser = loginHelper.GetExternalUser(provider, code);
            ApiToken token        = null;

            if (externalUser != null)
            {
                var apiUser = usersLogic.GetApiUserByExternalIdAndProvider(externalUser.ExternalId, externalUser.Provider)
                              ?? usersLogic.CreateUser(externalUser.ExternalId, externalUser.Provider);
                token = usersLogic.CreateTokenForApiUser(apiUser);
            }
            return(token);
        }
        private ActionResult Authenticate(string code, string provider, string redirectUrl)
        {
            ActionResult result;
            var          externalUser = loginHelper.GetExternalUser(provider, code);

            if (externalUser != null)
            {
                var apiUser = usersLogic.GetApiUserByExternalIdAndProvider(externalUser.ExternalId,
                                                                           externalUser.Provider);
                if (apiUser != null && apiUser.Name != null)
                {
                    if (String.IsNullOrEmpty(redirectUrl))
                    {
                        redirectUrl = Url.Action("Index", "Patterns");
                    }
                    result = new RedirectResult(redirectUrl);
                }
                else
                {
                    apiUser = usersLogic.CreateUser(externalUser.ExternalId, externalUser.Provider);
                    var registrationUrl = Url.Action("Register");
                    result = new RedirectResult(registrationUrl);
                }
                var token = usersLogic.CreateTokenForApiUser(apiUser);
                if (token != null)
                {
                    HttpContext.Response.Cookies.Add(new HttpCookie("Token", token.Id.ToString())
                    {
                        Expires = token.ExpirationTime
                    });
                }
            }
            else
            {
                result = RedirectToAction("Index", "Patterns");
            }

            return(result);
        }