5

我正在使用 C# 上的新 MIP SDK 构建 POC 应用程序。要求之一是使用存储在服务器上的用户名/密码。所有示例应用程序都使用 OAuth2 登录,并带有用于用户凭据输入的弹出窗口。我相信正确实施 IAuthDelegate 可能会有所帮助,但内联文档并没有太大帮助。在我的引擎初始化方法中,我遵循 SDK 示例

            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;

和 AuthDelegateImplementation 具有以下代码

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;
    }

谢谢你的帮助,C。

4

2 回答 2

2

好吧,显然没有使用用户/密码登录的 MIP SDK 方式。但是,我能够通过使用其他 Azure API 破解我的方式。我向 Azure REST 发送 REST 调用以获取访问和刷新令牌。该请求是从 IAuthDelegate 的实现发送的。您对方法 IAuthDelegate::AcquireToken 的实现将发送 REST 调用并返回访问令牌(字符串)。登录请求结构如下:

...
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);

响应结构是这样的:

   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; }
    }

希望这对未来的人有所帮助。

于 2019-03-04T15:16:53.097 回答
0

作为一般主体,MIP SDK 不关心您的登录 - 它只需要使用 IAuthDelegate 回调知道您的身份验证令牌。

您可以而且应该使用ADAL .Net以使用用户名和密码登录。您可以使用 .Net 示例来了解如何执行此操作

于 2019-04-15T06:16:55.653 回答