亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

微軟圖形 API 身份驗證

微軟圖形 API 身份驗證

C#
素胚勾勒不出你 2022-01-09 15:31:40
我實際上正在探索 microsoft graph api 并尋找一種無需任何交互即可以用戶身份進行身份驗證的解決方案,但找不到解決方案。我發現的每個代碼示例都需要用戶交互才能登錄 microsoft 以獲得令牌。是否有可能避免用戶交互?否則,我在此示例中找到了客戶端憑據流的解決方法:https : //github.com/microsoftgraph/console-csharp-snippets-sample 但如果我嘗試在 c# Asp.net mav applcition 或 Windows 窗體中實現此代碼應用程序我無法獲得應用程序令牌。如果我調試應用程序,它會一直等待令牌,但不會引發錯誤(病毒保護已停用)。有人對主要問題或我的解決方法有想法嗎?這是我嘗試獲取令牌的解決方法的代碼,但卡在 daemonClient.AcquireTokenForClientAsync 上。   public async Task<Users> GetUser(string Username)    {        MSALCache appTokenCache = new MSALCache(clientId);        ClientCredential clientdummy = new ClientCredential(clientSecret);        ConfidentialClientApplication daemonClient = new ConfidentialClientApplication(clientId, string.Format(AuthorityFormat, tenantId), redirectUri,                                                            clientdummy, null, null);        authenticate(daemonClient).Wait();        string token = authResult.AccessToken;        client = GetAuthenticatedClientForApp(token);        IGraphServiceUsersCollectionPage users = client.Users.Request().GetAsync().Result;    }    private async Task<AuthenticationResult> authenticate(ConfidentialClientApplication daemonClient)    {        authResult = await daemonClient.AcquireTokenForClientAsync(new[] { MSGraphScope });        return authResult;    }
查看完整描述

2 回答

?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

找到解決方法:通過 REST API 獲取令牌。在這里,我可以獲得用戶令牌或客戶端令牌來訪問圖形 api:


 var client = new RestClient("https://login.microsoftonline.com/" + domainname);

 var request = new RestRequest("/oauth2/token", Method.POST);   request.AddBody("grant_type", "client_credentials");

        request.AddParameter("client_id", clientId);

        request.AddParameter("client_secret", clientSecret);

        request.AddParameter("Resource", "https://graph.microsoft.com");

        request.AddParameter("scope", "[scopes]"); 

        IRestResponse response = client.Execute(request);

        //contains the token 

        var content = response.Content;


查看完整回答
反對 回復 2022-01-09
?
SMILET

TA貢獻1796條經驗 獲得超4個贊

根據您的描述,我假設您需要一個解決方案來驗證用戶而無需任何交互。


我們可以通過一些后臺服務或守護進程獲取訪問令牌。


更多細節,我們可以參考這個文檔。


根據我的測試,我們可以嘗試以下步驟:


首先,我們應該得到管理員的同意:


app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions

                                           {

                                               ClientId = clientId,

                                               Authority = authority,

                                               RedirectUri = redirectUri,

                                               PostLogoutRedirectUri = redirectUri,

                                               Scope = "openid profile",

                                               ResponseType = "id_token",

                                               TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false, NameClaimType = "name" },

                                               Notifications = new OpenIdConnectAuthenticationNotifications

                                                               {

                                                                   AuthenticationFailed = this.OnAuthenticationFailedAsync,

                                                                   SecurityTokenValidated = this.OnSecurityTokenValidatedAsync

                                                               }

                                           });




ConfidentialClientApplication daemonClient = new ConfidentialClientApplication(Startup.clientId, string.Format(AuthorityFormat, tenantId), Startup.redirectUri,

                                                                                       new ClientCredential(Startup.clientSecret), null, appTokenCache.GetMsalCacheInstance());

AuthenticationResult authResult = await daemonClient.AcquireTokenForClientAsync(new[] { MSGraphScope });

然后,我們可以使用此訪問令牌來使用 Graph API。


有關更多詳細信息,我們可以查看GitHub 上的v2.0 守護程序示例。


查看完整回答
反對 回復 2022-01-09
  • 2 回答
  • 0 關注
  • 230 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號