Search code examples
c#authenticationsdkmicrosoft-information-protection

Microsoft Information Protection SDK : using username/password to login


I am building a POC application using the new MIP SDK on C#. One of the requirements is to use username/password stored on the server. All the example applications are using OAuth2 login with a popup window for user credentials input. I believe that properly implementing the IAuthDelegate may help, but in-line documentation was not of much help. in my engine init method I am following the SDK example

            var authDelegate = new AuthDelegateImplementation(appInfo);

            //Initialize and instantiate the File Profile
            //Create the FileProfileSettings object
            var profileSettings = new FileProfileSettings(
                path: "mip_data", 
                useInMemoryStorage: true, 
                authDelegate: authDelegate,
                consentDelegate: new ConsentDelegateImplementation(), 
                applicationInfo: appInfo, 
                minimumLogLevel: LogLevel.Trace);

            Console.WriteLine("Load the Profile async and wait for the result");
            var fileProfile = Task.Run(async () => await MIP.LoadFileProfileAsync(profileSettings)).Result;

and the AuthDelegateImplementation having following code

public string AcquireToken(Identity identity, string authority, string resource)
    {
        AuthenticationContext authContext = new AuthenticationContext(authority);
        AuthenticationResult result = authContext.AcquireTokenAsync(
                                        resource: resource, 
                                        clientId: _appInfo.ApplicationId, 
                                        redirectUri: new Uri(redirectUri), 
                                        parameters: new PlatformParameters(PromptBehavior.Auto, null), 
                                        userId: UserIdentifier.AnyUser).Result;
        return result.AccessToken;
    }

Thanks for your help, C.


Solution

  • Well, apparently there is no MIP SDK way to login using the user/password. However, I was able to hack my way in by using other Azure API. I send a REST call to Azure REST to acquire the access and refresh tokens. The request is sent from an implementation of IAuthDelegate. Your implementation of method IAuthDelegate::AcquireToken will send the REST call and return the access token (string). The login request structure is as follows:

    ...
    client.BaseAddress = new Uri(String.Format("https://login.microsoftonline.com/{0}/oauth2/token", tenantId));
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
    ...
    string postBody = string.Format(
                            "grant_type=password&" +
                            "resource=https://psor.o365syncservice.com&" +
                            "username={0}&" +
                            "password={1}&" +
                            "client_id={2}", credentialsIdentity.Username, credentialsIdentity.Password, _appInfo.ApplicationId);
    

    the response structure is this:

       public class LoginResponse
        {
            public string token_type { get; set; }
            public string scope { get; set; }
            public string expires_in { get; set; }
            public string ext_expires_in { get; set; }
            public string expires_on { get; set; }
            public string not_before { get; set; }
            public string resource { get; set; }
            public string access_token { get; set; }
            public string refresh_token { get; set; }
        }
    

    Hope this will help someone down the road.