简体   繁体   中英

How to navigate to another website, pass login information and complete login form in C# ASP.Net MVC

I'm trying to create a kind of Auto Login feature in an internal ASP.Net MVC C# tool that will allow support users to quickly login to the various system they support.

I've been able to pass all the values needed to complete the login form via Ajax/jQuery to my controller and then to my Class. As seen below I can open the various URL's based on an if else, but not complete the login form.

I've been looking into httpwebrequests but am not sure how I would structure one to use the variables values (below) to complete the various login form field and then click the button.

public AutoLogin(string Environment, string Username, string OrgId, string UserId, string OrgKey)
    {
        var env = Environment;
        var user = Username;
        var org = OrgId;
        var userid = UserId;
        var orgKey = OrgKey;

        if (env == "system1")
        {
            string url = website;

            CookieContainer cookieJar = new CookieContainer();

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)";
            req.Method = "POST";
            req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            req.KeepAlive = true;
            req.Headers.Add("Keep-Alive: 300");
            req.AllowAutoRedirect = false;
            req.ContentType = "application/x-www-form-urlencoded";

            req.CookieContainer = cookieJar;

            string username = user;
            string pw = "password";
            string orgUid = orgKey;

            StreamWriter sw = new StreamWriter(req.GetRequestStream());
            sw.Write("userEmail=" + username + "&userPass=" + pw + "&orgUid=" + orgUid);
            sw.Close();

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();

            //Add cookies to CookieJar (Cookie Container)
            foreach (Cookie cookie in response.Cookies)
            {
                cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain));
            }

            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(1255));
            string tmp = reader.ReadToEnd();

            response.Close();
        }
        else if (env == "system2")
        {
            System.Diagnostics.Process.Start(url2);
        }
        else if (env == "system3")
        {
            System.Diagnostics.Process.Start(url3);
        }
        else
        {

        } 
    }

Update:

Added the httpwebrequet code I've been working on but when clicking the button front end it doesn't navigate/open the web page..

For internal sites, the easiest way is to use Windows Authentication in your web sites. Just unlock your PC with your password, then go to your internal web sites without login. Windows can do authentication 'under the hood'. Web.config:

<authentication mode="Windows" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM