public ActionResult Index(string state, string code)
        {
            string error        = string.Empty;
            var    sessionState = HttpContext.Session["state"];

            if (sessionState.Equals(state))
            {
                OidcModel oauthModel = SuperOfficeAuthHelper.GetOAuthModel(code);

                if (SuperOfficeAuthHelper.TryLogin(oauthModel, out error))
                {
                    var context = SuperOfficeAuthHelper.Context;

                    //Store the System User Information in the Database
                    CustomerDataSource dataSource = new CustomerDataSource();
                    var customer = dataSource.Customers.FirstOrDefault(c => c.ContextIdentifier == context.ContextIdentifier);

                    //var databaseContext = new PartnerDatabaseContext();
                    //var customer = databaseContext.Customers.FirstOrDefault(c => c.ContextIdentifier == context.ContextIdentifier);
                    if (customer == null)
                    {
                        dataSource.Customers.Add(new CustomerInfo
                        {
                            AssociateID       = context.AssociateId,
                            ContextIdentifier = context.ContextIdentifier,
                            IsActive          = true,
                            LastSync          = new DateTime(2000, 1, 1),
                            SystemUserToken   = context.SystemToken
                        });
                        dataSource.Save();
                    }

                    // Redirect to original request
                    var redirectUr = Session["RedirectUrl"] as string;

                    if (!String.IsNullOrEmpty(redirectUr))
                    {
                        return(Redirect(redirectUr));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
            }

            return(RedirectToAction("Welcome", "Home", new { Error = error }));
        }
Example #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            string error = string.Empty;

            SetupVisibleParts();

            var requestType = Context.Request.RequestType;

            if (requestType.ToUpper() == "POST")
            {
                var callbackModel = new CallbackModel
                {
                    Saml = Context.Request["saml"],
                    Jwt  = Context.Request["jwt"],
                };


                if (SuperOfficeAuthHelper.TryLogin(callbackModel, out error))
                {
                    var redirectUr = Context.Session["RedirectUrl"] as string;
                    Context.Session["RedirectUrl"] = "";

                    if (!String.IsNullOrEmpty(redirectUr))
                    {
                        Context.Response.Redirect(redirectUr);
                    }
                }
                else
                {
                    explanationText.Visible   = true;
                    explanationText.InnerText = "Login unsuccessful, reason: " + error;
                }
            }
            else
            {
                var code  = Context.Request["code"];
                var state = Context.Request["state"];

                var sessionState = Context.Session["state"] as string;

                if (sessionState.Equals(state))
                {
                    OidcModel oauthModel = SuperOfficeAuthHelper.GetOAuthModel(code);

                    if (SuperOfficeAuthHelper.TryLogin(oauthModel, out error))
                    {
                        var redirectUr = Context.Session["RedirectUrl"] as string;
                        Context.Session["RedirectUrl"] = "";

                        if (!String.IsNullOrEmpty(redirectUr))
                        {
                            Context.Response.Redirect(redirectUr);
                        }
                    }
                }
                else
                {
                    explanationText.Visible   = true;
                    explanationText.InnerText = "Login unsuccessful, reason: " + error;
                }
            }
        }
        public ActionResult Index(CallbackModel callbackModel)
        {
            if (callbackModel == null)
            {
                return(RedirectToAction("Index", "Home"));
            }



            /*
             * Here it is up to the partner intercept the callback from SuperID
             * and route the user to the correct partner application instance.
             *
             * This is also the opportunity for the Partner to create a system user
             * in the customers superoffice database for future use and storage.
             *
             * This is where any additional setup or configuration options are input into
             * the partners application for future use.
             */

            string error = string.Empty;

            if (SuperOfficeAuthHelper.TryLogin(callbackModel, out error))
            {
                var context = SuperOfficeAuthHelper.Context;
                //Store the System User Information in the Database
                CustomerDataSource dataSource = new CustomerDataSource();
                var customer = dataSource.Customers.FirstOrDefault(c => c.ContextIdentifier == context.ContextIdentifier);

                //var databaseContext = new PartnerDatabaseContext();
                //var customer = databaseContext.Customers.FirstOrDefault(c => c.ContextIdentifier == context.ContextIdentifier);
                if (customer == null)
                {
                    dataSource.Customers.Add(new CustomerInfo
                    {
                        AssociateID       = context.AssociateId,
                        ContextIdentifier = context.ContextIdentifier,
                        IsActive          = true,
                        LastSync          = new DateTime(2000, 1, 1),
                        SystemUserToken   = context.SystemToken
                    });
                    dataSource.Save();
                }

                // Redirect to original request
                var redirectUr = Session["RedirectUrl"] as string;

                if (!String.IsNullOrEmpty(redirectUr))
                {
                    return(Redirect(redirectUr));
                }
                else
                {
                    return(RedirectToAction("Index", "Home"));
                }
            }
            else
            {
                return(RedirectToAction("Welcome", "Home", new { Error = error }));
            }
        }
Example #4
0
        public ActionResult Index(string jwt)
        {
            if (String.IsNullOrWhiteSpace(jwt))
            {
                return(RedirectToAction("Index", "Home"));
            }

            /*
             * Here it is up to the partner intercept the callback from SuperID
             * and route the user to the correct partner application instance.
             *
             * This is also the opportunity for the Partner to create a system user
             * in the customers superoffice database for future use and storage.
             *
             * This is where any additional setup or configuration options are input into
             * the partners application for future use.
             */


            if (SuperOfficeAuthHelper.TryLogin(jwt))
            {
                var context = SuperOfficeAuthHelper.Context;

                AppDB.Initialize();
                AppDB.UpgradeDatabase();


                using (var db = new AppDB())
                {
                    bool newCustomer = false;
                    var  utcNow      = DateTime.UtcNow;
                    User user        = null;

                    var customer = db.Customers.FirstOrDefault(c => c.ContextIdentifier == context.ContextIdentifier);
                    if (customer == null)
                    {
                        customer = new Customer
                        {
                            ContextIdentifier = context.ContextIdentifier,
                            Registered        = utcNow,
                            LastUsed          = utcNow,
                        };
                        db.Customers.Add(customer);
                        newCustomer = true;
                        db.SaveChanges(); // Save one first....
                    }

                    if (!newCustomer)
                    {
                        user = customer.Users.FirstOrDefault(u => u.AssociateId == context.AssociateId);
                    }

                    if (user == null)
                    {
                        user = new User
                        {
                            Customer    = customer,
                            AssociateId = context.AssociateId,
                        };
                        db.Users.Add(user);
                    }
                    user.UserPrincipalName = context.Username;
                    user.Email             = context.Email;

                    if (newCustomer)
                    {
                        customer.RegisteredBy = user;
                    }

                    customer.LastUsedBy      = user;
                    customer.LastUsed        = utcNow;
                    customer.Name            = context.Company;
                    customer.NetServerUrl    = context.NetServerUrl;
                    customer.SystemUserToken = context.SystemToken;


                    db.SaveChanges();
                    SystemUserManager.ClearCachedItem(context.ContextIdentifier);
                }
                //Store the System User Information in the Database
                //var databaseContext = new PartnerDatabaseContext();
                //var customer = databaseContext.Customers.FirstOrDefault(c => c.ContextIdentifier == context.ContextIdentifier);
                //if (customer == null)
                //{
                //    databaseContext.Customers.Add(new CustomerInfo
                //    {
                //        AssociateID = context.AssociateId,
                //        ContextIdentifier = context.ContextIdentifier,
                //        IsActive = true,
                //        LastSync = new DateTime(2000, 1, 1),
                //        SystemUserToken = context.SystemToken
                //    });
                //    databaseContext.SaveChanges();
                //}

                // Redirect to original request
                var redirectUr = Session["RedirectUrl"] as string;

                if (!String.IsNullOrEmpty(redirectUr))
                {
                    return(Redirect(redirectUr));
                }
                else
                {
                    return(RedirectToAction("Index", "Home"));
                }
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }