Beispiel #1
0
        private void CreateAndLoginUser()
        {
            if (!IsValid)
            {
                return;
            }

            var createResult = OpenAuth.CreateUser(ProviderName, ProviderUserId, ProviderUserName, userName.Text);

            if (!createResult.IsSuccessful)
            {
                ModelState.AddModelError("UserName", createResult.ErrorMessage);
            }
            else
            {
                // User created & associated OK
                if (OpenAuth.Login(ProviderName, ProviderUserId, createPersistentCookie: false))
                {
                    RedirectToReturnUrl();
                }
            }
        }
        private void CreateAndLoginUser()
        {
            if (!IsValid)
            {
                return;
            }

            var createResult = OpenAuth.CreateUser(ProviderName, ProviderUserId, ProviderUserName, userName.Text);

            if (!createResult.IsSuccessful)
            {
                userNameMessage.Text = createResult.ErrorMessage;
            }
            else
            {
                // Creación y asociación de usuario correctas
                if (OpenAuth.Login(ProviderName, ProviderUserId, createPersistentCookie: false))
                {
                    RedirectToReturnUrl();
                }
            }
        }
Beispiel #3
0
    private void CreateAndLoginUser()
    {
        if (!IsValid)
        {
            return;
        }

        var createResult = OpenAuth.CreateUser(ProviderName, ProviderUserId, ProviderUserName, userName.Text);

        if (!createResult.IsSuccessful)
        {
            userNameMessage.Text = createResult.ErrorMessage;
        }
        else
        {
            // Pomyślnie utworzono i skojarzono użytkownika
            if (OpenAuth.Login(ProviderName, ProviderUserId, createPersistentCookie: false))
            {
                RedirectToReturnUrl();
            }
        }
    }
        private void CreateAndLoginUser()
        {
            if (!IsValid)
            {
                return;
            }

            var createResult = OpenAuth.CreateUser(ProviderName, ProviderUserId, ProviderUserName, userName.Text);

            if (!createResult.IsSuccessful)
            {
                userNameMessage.Text = createResult.ErrorMessage;
            }
            else
            {
                // Пользователь успешно создан и связан
                if (OpenAuth.Login(ProviderName, ProviderUserId, createPersistentCookie: false))
                {
                    RedirectToReturnUrl();
                }
            }
        }
Beispiel #5
0
        public ActionResult ExternalLoginCallback(string returnUrl)
        {
            string ProviderName = OpenAuth.GetProviderNameFromCurrentRequest();

            if (ProviderName == null || ProviderName == "")
            {
                NameValueCollection nvs = Request.QueryString;
                if (nvs.Count > 0)
                {
                    if (nvs["state"] != null)
                    {
                        NameValueCollection provideritem = HttpUtility.ParseQueryString(nvs["state"]);
                        if (provideritem["__provider__"] != null)
                        {
                            ProviderName = provideritem["__provider__"];
                        }
                    }
                }
            }

            GoogleOAuth2Client.RewriteRequest();

            var redirectUrl = Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl });
            var retUrl      = returnUrl;
            var authResult  = OpenAuth.VerifyAuthentication(redirectUrl);


            //string ProviderDisplayName = OpenAuth.GetProviderDisplayName(ProviderName);

            if (!authResult.IsSuccessful)
            {
                return(Redirect(Url.Action("Login", "Login")));
            }

            // User has logged in with provider successfully
            // Check if user is already registered locally
            //You can call you user data access method to check and create users based on your model
            if (OpenAuth.Login(authResult.Provider, authResult.ProviderUserId, createPersistentCookie: false))
            {
                return(Redirect(Url.Action("Index", "Home")));
            }

            //Get provider user details
            string ProviderUserId   = authResult.ProviderUserId;
            string ProviderUserName = authResult.UserName;

            string Email = null;

            if (Email == null && authResult.ExtraData.ContainsKey("email"))
            {
                Email = authResult.ExtraData["email"];
            }

            if (User.Identity.IsAuthenticated)
            {
                // User is already authenticated, add the external login and redirect to return url
                OpenAuth.AddAccountToExistingUser(ProviderName, ProviderUserId, ProviderUserName, User.Identity.Name);
                return(Redirect(Url.Action("Index", "Home")));
            }
            else
            {
                // User is new, save email as username
                string membershipUserName = Email ?? ProviderUserId;
                var    createResult       = OpenAuth.CreateUser(ProviderName, ProviderUserId, ProviderUserName, membershipUserName);

                if (!createResult.IsSuccessful)
                {
                    ViewBag.Message = "User cannot be created";
                    return(View());
                }
                else
                {
                    // User created
                    if (OpenAuth.Login(ProviderName, ProviderUserId, createPersistentCookie: false))
                    {
                        return(Redirect(Url.Action("Index", "Home")));
                    }
                }
            }
            return(View());
        }
Beispiel #6
0
        public ActionResult ExternalLoginCallback(string returnUrl)
        {
            string ProviderName = OpenAuth.GetProviderNameFromCurrentRequest();

            if (ProviderName == null || ProviderName == "")
            {
                NameValueCollection nvs = Request.QueryString;
                if (nvs.Count > 0)
                {
                    if (nvs["state"] != null)
                    {
                        NameValueCollection provideritem = HttpUtility.ParseQueryString(nvs["state"]);
                        if (provideritem["__provider__"] != null)
                        {
                            ProviderName = provideritem["__provider__"];
                        }
                    }
                }
            }

            GoogleOAuth2Client.RewriteRequest();

            var redirectUrl = Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl });
            var retUrl      = returnUrl;
            var authResult  = OpenAuth.VerifyAuthentication(redirectUrl);


            if (!authResult.IsSuccessful)
            {
                ViewBag.Message = "Not A Valid EmailID";
                return(Redirect(Url.Action("Account", "Login")));
            }

            // User has logged in with provider successfully
            // Check if user is already registered locally
            //You can call you user data access method to check and create users based on your model
            VC_LMSEntities d     = new VC_LMSEntities();
            string         email = authResult.ExtraData["email"];
            var            row   = d.UsersTables.Where(x => x.EMAIL == email).FirstOrDefault();

            if (row == null)
            {
                ViewBag.Message = "Email not in Database";
                return(RedirectToAction("Index"));
            }
            else
            {
                var id = row.EMPLOYEE_ID;
                //var manager = d.managers.Where(x => x.manager_emp_id == id).FirstOrDefault();
                //if ( manager!= null&&row.Status=="Active")
                //{

                //    Session["userID"] = row.EMPLOYEE_ID;
                //    Session["userName"] = row.FIRST_NAME;
                //    Session["Name"] = row.EMAIL;
                //    return RedirectToAction("Index", "manager");
                //}
                //else
                {
                    var row1 = d.approvers.Where(x => x.approver_id == row.EMPLOYEE_ID).FirstOrDefault();
                    if (row1 != null && row.Status == "Active")
                    {
                        Session["userID"]   = row.EMPLOYEE_ID;
                        Session["userName"] = row.FIRST_NAME;
                        Session["Name"]     = row.EMAIL;
                        return(RedirectToAction("Index", "Approver"));
                    }
                    else if (row.ROLES == "HR" && row.Status == "Active")
                    {
                        Session["userID"]   = row.EMPLOYEE_ID;
                        Session["userName"] = row.FIRST_NAME;
                        Session["Name"]     = row.EMAIL;
                        return(RedirectToAction("Index", "HR"));
                    }
                    else if (row.ROLES == "Employee" && row.Status == "Active")
                    {
                        Session["userID"]   = row.EMPLOYEE_ID;
                        Session["userName"] = row.FIRST_NAME;
                        Session["Name"]     = row.EMAIL;
                        return(RedirectToAction("Index", "Employee"));
                    }
                    else if (row.ROLES == "Admin" && row.Status == "Active")
                    {
                        Session["userID"]   = row.EMPLOYEE_ID;
                        Session["userName"] = row.FIRST_NAME;
                        Session["Name"]     = row.EMAIL;
                        return(RedirectToAction("Index", "Admin"));
                    }
                }
            }
            if (OpenAuth.Login(authResult.Provider, authResult.ProviderUserId, createPersistentCookie: false))
            {
                return(Redirect(Url.Action("Index", "Home")));
            }

            //Get provider user details
            string ProviderUserId   = authResult.ProviderUserId;
            string ProviderUserName = authResult.UserName;

            string Email = null;

            if (Email == null && authResult.ExtraData.ContainsKey("email"))
            {
                Email = authResult.ExtraData["email"];
            }

            if (User.Identity.IsAuthenticated)
            {
                // User is already authenticated, add the external login and redirect to return url
                OpenAuth.AddAccountToExistingUser(ProviderName, ProviderUserId, ProviderUserName, User.Identity.Name);
                return(Redirect(Url.Action("Index", "Home")));
            }
            else
            {
                // User is new, save email as username
                string membershipUserName = Email ?? ProviderUserId;
                var    createResult       = OpenAuth.CreateUser(ProviderName, ProviderUserId, ProviderUserName, membershipUserName);

                if (!createResult.IsSuccessful)
                {
                    ViewBag.Message = "User cannot be created";
                    return(View());
                }
                else
                {
                    // User created
                    if (OpenAuth.Login(ProviderName, ProviderUserId, createPersistentCookie: false))
                    {
                        return(Redirect(Url.Action("Index", "Home")));
                    }
                }
            }
            return(View());
        }
Beispiel #7
0
        public ActionResult ExternalLoginCallback(string returnUrl)
        {
            string ProviderName = OpenAuth.GetProviderNameFromCurrentRequest();

            if (ProviderName == null || ProviderName == "")
            {
                NameValueCollection nvs = Request.QueryString;
                if (nvs.Count > 0)
                {
                    if (nvs["state"] != null)
                    {
                        NameValueCollection provideritem = HttpUtility.ParseQueryString(nvs["state"]);
                        if (provideritem["microsoft"] != null)
                        {
                            ProviderName = provideritem["microsoft"];
                        }
                    }
                }
            }

            var redirectUrl = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }, this.Request.Url.Scheme);

            if (ProviderName == null)
            {
                ProviderName = "microsoft";
            }

            MicrosoftClient msClient   = OpenAuth.AuthenticationClients.GetByProviderName(ProviderName) as MicrosoftClient;
            var             authResult = msClient.VerifyAuthentication(new HttpContextWrapper(HttpContext.ApplicationInstance.Context), new Uri(redirectUrl));

            if (!authResult.IsSuccessful)
            {
                return(Redirect(Url.Action("Login", "Account")));
            }

            // User has logged in with provider successfully
            // Check if user is already registered locally
            if (OpenAuth.Login(authResult.Provider, authResult.ProviderUserId, createPersistentCookie: false))
            {
                return(Redirect(Url.Action("Index", "Home")));
            }
            //Get provider user details
            string ProviderUserId   = authResult.ProviderUserId;
            string ProviderUserName = authResult.UserName;
            string FirstName        = null;
            string LastName         = null;

            string Email = null;

            if (FirstName == null && authResult.ExtraData.ContainsKey("firstname"))
            {
                FirstName = authResult.ExtraData["firstname"];
            }
            if (LastName == null && authResult.ExtraData.ContainsKey("lastname"))
            {
                LastName = authResult.ExtraData["lastname"];
            }
            // call extension method to get email
            ExtendedMicrosoftClientUserData data = msClient.GetExtraData(authResult.ExtraData["accesstoken"]);

            Email = data.Emails.Preferred;

            if (User.Identity.IsAuthenticated)
            {
                // User is already authenticated, add the external login and redirect to return url
                OpenAuth.AddAccountToExistingUser(ProviderName, ProviderUserId, ProviderUserName, User.Identity.Name);
                return(Redirect(Url.Action("Index", "Home")));
            }
            else
            {
                // User is new, save email as username
                string membershipUserName = Email ?? ProviderUserId;
                var    createResult       = OpenAuth.CreateUser(ProviderName, ProviderUserId, ProviderUserName, membershipUserName);

                if (!createResult.IsSuccessful)
                {
                    ViewBag.Message = "User cannot be created";
                    return(View());
                }
                else
                {
                    // User created
                    if (OpenAuth.Login(ProviderName, ProviderUserId, createPersistentCookie: false))
                    {
                        return(Redirect(Url.Action("Index", "Home")));
                    }
                }
            }
            return(View());
        }