Con*_*ant 5 c# sdk login 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;
Run Code Online (Sandbox Code Playgroud)
和 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;
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助,C。
嗯,显然没有 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);
Run Code Online (Sandbox Code Playgroud)
响应结构是这样的:
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; }
}
Run Code Online (Sandbox Code Playgroud)
希望这能帮助某人。
| 归档时间: |
|
| 查看次数: |
429 次 |
| 最近记录: |