c# - 微软信息保护 SDK : using username/password to login

标签 c# authentication sdk microsoft-information-protection

我正在 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.

最佳答案

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

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

希望这对以后的人有所帮助。

关于c# - 微软信息保护 SDK : using username/password to login,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54855754/

相关文章:

c# - 如何使用 C# 对 Picasa 网络相册数据 API 进行 OAuth2 身份验证?

php - 我的 php 登录表单有什么问题?

python - azure-sdk-for-python : get list of managed disks from a specified resource group

c# - Xamarin.Forms(可为空)DatePicker : Workaround for missing OK and cancel events

c# - 为 RESTful 服务格式化 DateTime 的最佳方式?

javascript - 如何用jquery隐藏模式?

android.os.Build.Version 仅使用 4.4.2

c# - 按钮单击Xamarin Android项目仅工作一次

node.js - 将身份验证提供程序添加到 Azure 移动服务 Nodejs 后端

ios - iOS 中应用程序崩溃时如何将应用程序带到应用程序主屏幕