ADAL v3: How to authenticate using UserPasswordCredential?

前端 未结 2 1182
清歌不尽
清歌不尽 2020-12-03 04:03

ADAL v3 has the UserPasswordCredential class, but I cannot find a working implementation. There\'s no AcquireToken overload which accepts a UserPasswordCredential or UserCre

2条回答
  •  被撕碎了的回忆
    2020-12-03 04:40

    If you were developing with client app, you can refer the code below to acquire the token:

    string authority = "https://login.microsoftonline.com/xxxx.onmicrosoft.com";
    string resrouce = "https://graph.windows.net";
    string clientId = "";
    string userName = "";
    string password = "";
    UserPasswordCredential userPasswordCredential = new UserPasswordCredential(userName,password);
    AuthenticationContext authContext = new AuthenticationContext(authority);
    var token= authContext.AcquireTokenAsync(resrouce,clientId, userPasswordCredential).Result.AccessToken;
    

    And if you were developing with web app( this is not common scenario), there is no such method in ADAL V3 to support this scenario. As a workaround, you may construct the request yourself. Here is an example for your reference:

    POST: https://login.microsoftonline.com/xxxxx.onmicrosoft.com/oauth2/token
    
    Content-Type: application/x-www-form-urlencoded
    resource={resource}&client_id={clientId}&grant_type=password&username={userName}&password={password}&scope=openid&client_secret={clientSecret}
    

提交回复
热议问题