大家好我是新來的,在我的項目中使用API
。 我使用Asp.Net與C#
,它沒有MVC architecture
。 我的客戶需要將Office 365 API集成到項目中,以便任何想要訪問我們的服務的用戶都可以通過他們的Office 365憑證登錄。 雖然我在互聯網上搜索它說我需要ASP.net與MVC
來源使用office 365
來源。請建議可以做些什麼。我想爲我的Asp.net使用office 365 Api和C#非mvc項目
-1
A
回答
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
相關問題
- 1. C#.Net Office 365 API入門
- 2. office 365日曆api C#
- 3. WOPI使用office 365
- 4. Office 365/Sharepoint API
- 5. OneNote Rest API和Office 365
- 6. 使用Office 365 REST API無法獲取項目附件
- 7. Office 365的REST API
- 8. Office 365 API MVC身份驗證
- 9. 如何使用Office 365的
- 10. Office 365 Oauth web api
- 11. Office 365日曆API
- 12. 從服務中使用office 365 api
- 13. 開始使用Office 365 REST API
- 14. 使用Office 365 Unified API搜索
- 15. 如果我們想要將用戶添加爲Office 365日曆事件的參與者,是否必須在Office 365中將用戶設置爲「聯繫人」?
- 16. 在沒有Aceess目錄的情況下使用office 365 API
- 17. 在web api項目中使用asp.net mvc
- 18. 我想使用Windows7的視覺風格在我的C#項目
- 19. 用於Office 365的Microsoft Office自動化
- 20. c#如何使用microsoft graph api獲取office 365用戶照片
- 21. Office 365:使用Power Shell腳本連接到Office Online 365服務
- 22. 在我們的asp.net mvc web項目中使用MVC SitemapProvider與AppFabric
- 23. Office 365 API集成前提
- 24. Office 365 Unified Api(測試版)
- 25. Office 365共享郵箱API
- 26. Office 365 API - 自動登錄
- 27. Office 365 OneDrive REST API - 「invalid_client」
- 28. Office 365統一API(預覽)
- 29. 我們可以每天進行30,000次Office 365 API調用嗎?
- 30. 的Microsoft Office 365中的iOS