如何通过C#对Office 365门户进行身份验证

use*_*117 13 c# federated-identity office365

我有一个与Microsoft的Exchange租户(company.onmicrosoft.com),一个管理员帐户(admin@company.onmicrosoft.com),以及管理我的(设置)的管理页面.

最近,有一个项目我通过C#从管理页面自动解析一些Web数据.我阅读了很多与SharePoint相关的文章,并阅读了有关STS,SRF文件和主动/被动联合服务的文章.

那么,让我们来看看:

我从https://login.microsoftonline.com/login.srf开始,但是Fiddler意识到我可以直接跳到https://login.microsoftonline.com/ppsecure/post.srf并获得相同的结果.

所以,我去登录,输入我的凭据,然后转发给我管理页面.很简单吧?

所以,我决定复制网络流量,并依赖以下帖子和示例:

所有这些网站在屏幕抓取认证,ADFS和一堆其他有用信息上都有很棒的C#示例.问题是所有这些都是针对SharePoint的.

所以,我混合并匹配代码并提出以下内容:

static void Main3()
{
    CookieContainer cookies = new CookieContainer();
    //using Bungie example from http://stackoverflow.com/questions/2508656/logging-into-a-site-that-uses-live-com-authentication
    //Uri url = new Uri("https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1268167141&rver=5.5.4177.0&wp=LBI&wreply=http:%2F%2Fwww.bungie.net%2FDefault.aspx&id=42917");

    Uri url = new Uri("https://portal.microsoftonline.com/");
    HttpWebRequest http = (HttpWebRequest)HttpWebRequest.Create(url);

    http.Timeout = 30000;
    http.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
    http.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    http.AllowAutoRedirect = false;
    http.Headers.Add("Accept-Language", "en-us,en;q=0.5");
    http.Headers.Add("Accept-Encoding", "gzip, deflate");  //this option creates the two cookies but loads garbage HTML.  Removing this option allows the HTML to load normally
    http.KeepAlive = true;
    http.CookieContainer = new CookieContainer();
    http.Method = WebRequestMethods.Http.Get;

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

    //gets the cookies (they are set in the eighth header)
    string[] strCookies = response.Headers.GetValues(8);
    response.Close();


    Cookie manualCookie;
    string sManualCookiesString = "MSPRequ|lt=1389810702&id=271346&co=1;MSPOK|$uuid-02eeaf29-b8a5-441f-a6a6-319ed926d8bc$uuid-7f106156-1398-405f-83e5-61f177c7be25$uuid-3d2f189d-8f79-4216-97cf-23c5c22ff8ad$uuid-b93c9354-5802-4c82-ac7d-7838d2f7bdbc$uuid-071c3106-1c97-4e1e-930c-36f33b6f0b93; MSPShared|1; MSPSoftVis|@:@; MSPBack|1389810501";


    //Manually insert the cookies since the request only returns two cookies and we need six cookies
    foreach (string sCookieAndValue in sManualCookiesString.Split(';'))
    {
        string sCookieName = sCookieAndValue.Split('|')[0].Trim();
        string sCookieValue = sCookieAndValue.Split('|')[1].Trim();
        manualCookie = new Cookie(sCookieName, "\"" + sCookieValue + "\"");

        Uri manualURL = new Uri("http://login.microsoftonline.com");
        http.CookieContainer.Add(manualURL, manualCookie);
    }

    /*      //Removed because the above request only returns MSPRequ and MSPOK cookies but leaves out the others.  It's obviously broken :(
                string name, value;
                for (int i = 0; i < strCookies.Length; i++)
                {
                    name = strCookies[i].Substring(0, strCookies[i].IndexOf("="));
                    value = strCookies[i].Substring(strCookies[i].IndexOf("=") + 1, strCookies[i].IndexOf(";") - strCookies[i].IndexOf("=") - 1);
                    manualCookie = new Cookie(name, "\"" + value + "\"");

                    Uri manualURL = new Uri("http://login.microsoftonline.com");
                    //http.CookieContainer.Add(manualURL, manualCookie);
                }
     */

    //stores the cookies to be used later
    cookies = http.CookieContainer;

    http = (HttpWebRequest)HttpWebRequest.Create(url);
    response = (HttpWebResponse)http.GetResponse();
    StreamReader readStream = new StreamReader(response.GetResponseStream());
    string HTML = readStream.ReadToEnd();
    readStream.Close();

    //Get the PPSX value: these values are a bit strange and could indicate progress since I've seen a few of the following:
    //      P, Pa, Pas, Pass, Passp, Passpo, Passport, PassportRN
    //  As you can see it adds one letter at a time which could indicate where in the login process it is, but I don't know
    string PPSX = HTML.Remove(0, HTML.IndexOf("PPSX"));
    PPSX = PPSX.Remove(0, PPSX.IndexOf("value") + 7);
    PPSX = PPSX.Substring(0, PPSX.IndexOf("\""));
    //PPSX = "PassP"; //debug

    //Get this random PPFT value
    string PPFT = HTML.Remove(0, HTML.IndexOf("PPFT"));
    PPFT = PPFT.Remove(0, PPFT.IndexOf("value") + 7);
    PPFT = PPFT.Substring(0, PPFT.IndexOf("\""));

    //Get the random URL you POST to
    //string POSTURL = HTML.Remove(0, HTML.IndexOf("https://login.microsoftonline.com/ppsecure/post.srf?wa=wsignin1.0&rpsnv=2&ct="));
    //POSTURL = POSTURL.Substring(0, POSTURL.IndexOf("\""));
    //debug:
    //based on Fiddler, this page is the next page that's loaded
    string POSTURL = "https://login.microsoftonline.com/GetUserRealm.srf?login=admin%company.onmicrosoft.com&handler=1&extended=1 ";

    //POST with cookies
    HttpWebRequest http2 = (HttpWebRequest)HttpWebRequest.Create(POSTURL);

    //http.AllowAutoRedirect = false;
    http2.Accept = "application/json, text/javascript, */*; q=0.01";
    http2.Headers.Add("Accept-Encoding", "gzip, deflate");
    http2.Headers.Add("Accept-Language", "en-us,en;q=0.5");
    http2.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";

    //http.ContentLength = 0;
    http2.KeepAlive = true;
    http.CookieContainer = cookies;
    http2.Referer = "https://login.microsoftonline.com/ppsecure/post.srf";
    http2.Method = WebRequestMethods.Http.Post;

    Stream ostream = http2.GetRequestStream();

    //used to convert strings into bytes
    System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

    //Post information found via Fiddler. Values have been altered for anonymity
    byte[] buffer = encoding.GetBytes(
                                                                        "login=" + sUsername
                                                                        + "&passwd=" + sPassword
                                                                        + "&PPSX=" + PPSX
                                                                        + "&PPFT=" + PPFT
                                                                        + "&n1=107313"
                                                                        + "&n2=-1389941230500"
                                                                        + "&n3=-1389941230500"
                                                                        + "&n4=112373"
                                                                        + "&n5=112373"
                                                                        + "&n6=112373"
                                                                        + "&n7=112373"
                                                                        + "&n8=NaN"
                                                                        + "&n9=112373"
                                                                        + "&n10=112360"
                                                                        + "&n11=112358"
                                                                        + "&n12=112323"
                                                                        + "&n13=112324"
                                                                        + "&n14=112396"
                                                                        + "&n15=26"
                                                                        + "&n16=11239"
                                                                        + "&n17=112369"
                                                                        + "&n18=112315"
                                                                        + "&n19=880.9711230112345"
                                                                        + "&n20=1"
                                                                        + "&n21=1"
                                                                        + "&n22=1381236981084.398"
                                                                        + "&n23=1"
                                                                        + "&n24=46.789501123103664"
                                                                        + "&n25=0"
                                                                        + "&n26=0"
                                                                        + "&n27=0"
                                                                        + "&n28=0"
                                                                        + "&n29=-1318912363023"
                                                                        + "&n30=-1318912363023"
                                                                        + "&n31=false"
                                                                        + "&n32=false"
                                                                        + "&type=11"
                                                                        + "&LoginOptions=3" //this is 2 sometimes
                                                                        + "&NewUser=1"
                                                                        + "&idsbho=1"
                                                                        + "&PwdPad="
                                                                        + "&sso="
                                                                        + "&vv="
                                                                        + "&uiver=1"
                                                                        + "&i12=1"
                                                                        + "&i13=Firefox"
                                                                        + "&i14=26.0"
                                                                        + "&i15=1480"
                                                                        + "&i16=964"
                                                                        );
    ostream.Write(buffer, 0, buffer.Length);
    ostream.Close();

    HttpWebResponse response2 = (HttpWebResponse)http.GetResponse();
    readStream = new StreamReader(response2.GetResponseStream());
    HTML = readStream.ReadToEnd();

    response2.Close();
    ostream.Dispose();
    foreach (Cookie cookie in response2.Cookies) //this returns no cookies
    {
        Console.WriteLine(cookie.Name + ": ");
        Console.WriteLine(cookie.Value);
        Console.WriteLine(cookie.Expires);
        Console.WriteLine();
    }

    POSTURL = "https://login.microsoftonline.com/ppsecure/post.srf?bk=1389198967";
    //POST with cookies
    http = (HttpWebRequest)HttpWebRequest.Create(POSTURL);

    http.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
    http.AllowAutoRedirect = false;
    http.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    http.Headers.Add("Accept-Language", "en-us,en;q=0.5");
    http.KeepAlive = true;
    http.CookieContainer = cookies;
    http.Referer = "https://login.microsoftonline.com/ppsecure/post.srf";
    http.Method = WebRequestMethods.Http.Post;

    ostream = http.GetRequestStream();

    //used to convert strings into bytes
    encoding = new System.Text.ASCIIEncoding();

    //Post information
    ostream.Write(buffer, 0, buffer.Length);
    ostream.Close();

    HttpWebResponse response3 = (HttpWebResponse)http.GetResponse();
    readStream = new StreamReader(response3.GetResponseStream());
    HTML = readStream.ReadToEnd();

    response3.Close();
    ostream.Dispose();
    foreach (Cookie cookie in response3.Cookies)  //sadly this returns no cookies when it should have 18 cookies in addition to a bunch of BOX.CacheKey cookies
    {
        Console.WriteLine(cookie.Name + ": ");
        Console.WriteLine(cookie.Value);
        Console.WriteLine(cookie.Expires);
        Console.WriteLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

有谁知道网站如何工作以提供我失败的地方的一些指导?该网站还使用JavaScript创建cookie(您可以访问第一页并输入随机地址,您将看到一些点在用户名字段中从左向右移动.

也许我采取了错误的方法,但任何帮助将不胜感激.如果需要,我可以提供Fiddler摘要会话日志.

谢谢!

PS对于实际期间角色的所有.com替换感到抱歉,但我没有足够的声望点.只需搜索[dot] com并替换为.com即可撤消.

Mat*_*att 1

您想要登录 Office 365 门户,而不是 SharePoint 管理门户,因此有关如何登录 SharePoint 的文章对您的情况没有帮助。

我搜索了一下,我认为要登录Office 365门户,您首先需要Microsoft Online Services登录助手

微软提供了一些关于如何管理office365用户的示例:

  1. Office 365:Office 365 仪表板
  2. Office 365:管理 Office 365 的许可证和订阅

这两个示例都是基于 Microsoft Online Services Sign-In Assistant 和 powershell cmdlet ,因此在 C# 中,我们可以使用该组件中的功能进行登录。但似乎关于这个组件的文档并不多。所以如果你会使用Powershell,你可以按照示例来做。如果不能,您可能需要深入研究登录命令并了解如何在 C# 中实现它。