2016-03-02 18 views
-1

大家好我是新來的,在我的項目中使用API。 我使用Asp.Net與C#,它沒有MVC architecture。 我的客戶需要將Office 365 API集成到項目中,以便任何想要訪問我們的服務的用戶都可以通過他們的Office 365憑證登錄。 雖然我在互聯網上搜索它說我需要ASP.net與MVC來源使用office 365來源。請建議可以做些什麼。我想爲我的Asp.net使用office 365 Api和C#非mvc項目

回答

0

您可以使用Active Directory身份驗證庫.NET輕鬆地將用戶身份驗證到雲或本地Active Directory(AD),然後獲取用於保護API調用的訪問令牌。在Web表單,下面的代碼是供你參考:

 protected void Page_Load(object sender, EventArgs e) 
     { 
     string authCode = Request.Params["code"]; 
     if (!string.IsNullOrEmpty(authCode)) 
     { 
      Authorize(authCode); 
     } 

     string token = (string)Session["access_token"]; 
     if (string.IsNullOrEmpty(token)) 
     { 
      return; 
     } 
     try 
     { 
      // get user name 
      getUserName(token); 
     } 
     catch (AdalException ex) 
     { 

     } 
    } 

    public void getUserName(string token) 
    { 
     using (var client = new HttpClient()) 
     { 
      //Enable signon and read users' profile 
      using (var request = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/beta/me")) 
      { 
       request.Headers.Add("Authorization", "Bearer " + token); 
       request.Headers.Add("Accept", "application/json;odata.metadata=minimal"); 
       using (var response = client.SendAsync(request).Result) 
       { 
        if (response.StatusCode == HttpStatusCode.OK) 
        { 
         var json = JObject.Parse(response.Content.ReadAsStringAsync().Result); 
         Response.Write(json["displayName"].ToString()); 
        } 
       } 
      } 
     } 
    } 

    public void Authorize(string authCode) { 
     AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/common"); 

     // The same url we specified in the auth code request 
     string redirectUri = "http://localhost:55065/Default.aspx"; 

     // Use client ID and secret to establish app identity 
     ClientCredential credential = new ClientCredential(ConfigurationManager.AppSettings["ClientID"], ConfigurationManager.AppSettings["ClientSecret"]); 

     try 
     { 
      // Get the token 
      var authResult = authContext.AcquireTokenByAuthorizationCode(
       authCode, new Uri(redirectUri), credential, "https://graph.microsoft.com/"); 

      // Save the token in the session 
      Session["access_token"] = authResult.AccessToken; 


      Response.Redirect(redirectUri.ToString()); 
     } 
     catch (AdalException ex) 
     { 
      //return Content(string.Format("ERROR retrieving token: {0}", ex.Message)); 
     } 
    } 
    public void signin() 
    { 
     var authContext = new AuthenticationContext("https://login.microsoftonline.com/common"); 


     // The url in our app that Azure should redirect to after successful signin 
     string redirectUri = "http://localhost:55065/Default.aspx"; 


     // Generate the parameterized URL for Azure signin 
     Uri authUri = authContext.GetAuthorizationRequestURL("https://graph.microsoft.com/", ConfigurationManager.AppSettings["ClientID"], 
      new Uri(redirectUri), UserIdentifier.AnyUser, null); 

     // Redirect the browser to the Azure signin page 
     Response.Redirect(authUri.ToString()); 
    } 

您也可以參考以下鏈接以獲得與O365 API一些例子在GitHub上: https://github.com/dream-365/OfficeDev-Samples/tree/master/samples/Office365DevQuickStart

但我建議你可以嘗試使用ASP.NET MVC設計時考慮到了關注點和可測試性的分離,並且您會在這裏找到許多帶有O365的MVC示例: Office 365 API code samples and videos