2016-05-17 152 views
0

我正嘗試在ASP.Net MVC中創建一個頁面來重置當前用戶的密碼。我正在使用Azure活動目錄進行用戶驗證。要訪問用戶的AD信息,我正在使用C#Graph API客戶端。我的代碼基於在GitHub使用Azure AD Graph客戶端API更改用戶密碼的權限問題

上找到的樣本。我可以對用戶的信息(如城市,州,郵件)進行更改。但是,當我嘗試使用用戶對象上的PasswordProfile屬性更改密碼時,出現錯誤,表示我的權限不足。我正在嘗試將密碼更改爲應用程序,並且我認爲權限問題的來源與應用程序有關。

我找到了下面的PowerShell腳本,它應該將公司管理員角色添加到應用程序中。但是,對Get-MsolServicePrincipal的調用不會返回任何內容。查看命令的輸出,我沒有看到任何類似於我的應用程序名稱的條目。

#----------------------------------------------------------- 
# This will prompt you for your tenant's credential 
# You should be able to use your your Azure AD administrative user name 
# (in the [email protected] format) 
#----------------------------------------------------------- 
import-module MSOnline 
Connect-MsolService 

#----------------------------------------------------------- 
# Replace the Application Name with the name of your 
# Application Service Principal 
#----------------------------------------------------------- 
$displayName = "My Azure AD Application" 
$objectId = (Get-MsolServicePrincipal -SearchString $displayName).ObjectId 

#----------------------------------------------------------- 
# This will add your Application Service Prinicpal to 
# the Company Administrator role 
#----------------------------------------------------------- 
$roleName = "Company Administrator"    
Add-MsolRoleMember -RoleName $roleName -RoleMemberType ServicePrincipal -RoleMemberObjectId $objectId 

我想我的第一個問題是我正確的權限問題是與應用程序?

其次,我應該設置$ displayName變量的值是多少?

回答

2

您需要確保Azure Active Directory中的應用程序配置具有適當的權限設置。使用上面的內容添加權限是過度的。您應該只將所需的權限添加到目錄中的應用程序。

作爲一個起點,我建議你在這裏查看Graph API Consent Permission博客文章。您只能使用傳統權限作爲帳戶或全局管理員重置密碼,但您應該分配應用程序權限。

「讀取和寫入目錄數據」用於提供此權限,但我相信這已被刪除。現在我相信用戶必須同意使用OAuth身份驗證流程才能重置密碼。

有關詳細信息,請參閱當前登錄用戶的changePassword方法。我有一個自定義桌面應用程序供用戶重置自己的密碼。

在我的應用程序中,我讓用戶使用其現有密碼進行身份驗證以獲取訪問令牌(這也有助於在更改密碼之前驗證其當前憑據)。

var authority = string.Format(CultureInfo.InvariantCulture, "https://login.microsoftonline.com/" + Domain); 
var ctx = new AuthenticationContext(authority, false); 
var result = ctx.AcquireToken("https://graph.windows.net", "ClientID", credential); 
return result.AccessToken; 

憑證屬性是使用用戶UPN和密碼的UserCredential類型。然後我傳遞一個Web請求來更改密碼。

var client = new HttpClient(); 
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", UserAccessToken); 
var requestUri = $"https://graph.windows.net/me/changePassword?api-version={Constants.ApiVersion}"; 
var pwdObject = new { currentPassword = curPassword, newPassword = newPass }; 
var body = new JavaScriptSerializer().Serialize(pwdObject); 
var response = client.PostAsync(new Uri(requestUri), new StringContent(body, Encoding.UTF8, "application/json")).Result; 

這將返回一個HTTP 204,如果請求是成功的,我也陷阱獲得用戶令牌,因爲這當AADSTS50126異常指示獲取令牌當憑證無效。

+0

您可以擴展如何創建UserCredential?我所看到的唯一屬性和構造函數是用戶名。桌面應用程序和Web應用程序庫有區別嗎? –

+0

我不認爲存在差異否,下面是使用'UserCredential'的一些信息... http://www.cloudidentity.com/blog/2014/07/08/using-adal-net-to-authenticate -users-via-usernamepassword /基本用法雖然是...'var credential = new UserCredential(「username」,「password」);' –

+0

由於某種原因,UserCredential對我來說沒有兩個構造函數,但UserPasswordCredential可以做到這一點。在調整我的常量和權限之後,我讓它工作。謝謝。 –