C# SSO for Microsoft Graph调用UWP for Sharepoint Online不返回任何令牌
所以我一直在为这个UWP应用程序工作。我在我的开发者网站上运行了一个测试程序,并使它运行得很好。我重新创建了UWP应用程序,以便在我公司的平台上访问它,我遇到了一个障碍。当我试图提交数据时,我得到一个MsalUIRequiredException错误C# SSO for Microsoft Graph调用UWP for Sharepoint Online不返回任何令牌,c#,uwp,single-sign-on,microsoft-graph-api,C#,Uwp,Single Sign On,Microsoft Graph Api,所以我一直在为这个UWP应用程序工作。我在我的开发者网站上运行了一个测试程序,并使它运行得很好。我重新创建了UWP应用程序,以便在我公司的平台上访问它,我遇到了一个障碍。当我试图提交数据时,我得到一个MsalUIRequiredException错误 {Microsoft.Identity.Client.MsalUiRequiredException: Null user was passed in AcquiretokenSilent API. Pass in a user object or
{Microsoft.Identity.Client.MsalUiRequiredException: Null user was passed in AcquiretokenSilent API. Pass in a user object or call acquireToken authenticate.
at Microsoft.Identity.Client.Internal.Requests.SilentRequest..ctor(AuthenticationRequestParameters authenticationRequestParameters, Boolean forceRefresh)
at Microsoft.Identity.Client.ClientApplicationBase.<AcquireTokenSilentCommonAsync>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Identity.Client.ClientApplicationBase.<AcquireTokenSilentAsync>d__31.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at AshburnGeneratorApp.Authentication.<AquireTokenAsync>d__0.MoveNext()
ErrorCode: user_null
StatusCode: 0
Claims: }
其中“companyname”是我实际的公司名称
如果我在图形资源管理器上运行GET,该链接工作得很好。所以我知道我错过了一些简单的东西
以下代码启动身份验证:
private async void SubmitButton_Click(object sender, RoutedEventArgs e)
{
var (authResult, message) = await Authentication.AquireTokenAsync();
ResultText.Text = message;
if (authResult != null)
{
await SubmitDataWithTokenAsync(submiturl, authResult.AccessToken);
}
}
这将调用此代码:
public static async Task<(AuthenticationResult authResult, string message)> AquireTokenAsync()
{
AuthenticationResult authResult = null;
string message = String.Empty;
string[] scopes = App.scopes;
try
{
authResult = await App.PublicClientApp.AcquireTokenSilentAsync(scopes, App.PublicClientApp.Users.FirstOrDefault());
}
catch (MsalUiRequiredException ex)
{
// A MsalUiRequiredException happened on AcquireTokenSilentAsync. This indicates you need to call AcquireTokenAsync to acquire a token
System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
try
{
authResult = await App.PublicClientApp.AcquireTokenAsync(scopes);
}
catch (MsalException msalex)
{
message = $"Error Acquiring Token:{System.Environment.NewLine}{msalex}";
}
}
catch (Exception ex)
{
message = $"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}";
}
return (authResult, message);
}
最后一段代码
private static string ClientId = "87f68831-d1ea-493c-8b94-e61fcd1c4a08";
public static PublicClientApplication PublicClientApp { get; } = new PublicClientApplication(ClientId);
我知道这很简单。我没有为v2.0注册应用程序。一旦我这么做了,一切都很顺利。您成功获得访问令牌了吗?你能试着创建一个简单的示例来帮助我看到你在我这边的问题吗?请注意,在代码示例中,请注意您的隐私。您也可以参考代码。我看到,即使它在我的开发中的另一个实例上工作,但在这个实例中却不能正常工作。我正在研究代码以找出答案。我会让你知道的。
public static async Task<(AuthenticationResult authResult, string message)> AquireTokenAsync()
{
AuthenticationResult authResult = null;
string message = String.Empty;
string[] scopes = App.scopes;
try
{
authResult = await App.PublicClientApp.AcquireTokenSilentAsync(scopes, App.PublicClientApp.Users.FirstOrDefault());
}
catch (MsalUiRequiredException ex)
{
// A MsalUiRequiredException happened on AcquireTokenSilentAsync. This indicates you need to call AcquireTokenAsync to acquire a token
System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
try
{
authResult = await App.PublicClientApp.AcquireTokenAsync(scopes);
}
catch (MsalException msalex)
{
message = $"Error Acquiring Token:{System.Environment.NewLine}{msalex}";
}
}
catch (Exception ex)
{
message = $"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}";
}
return (authResult, message);
}
public static string[] scopes = new string[] { "user.ReadWrite", "Sites.ReadWrite.All" };
private static string ClientId = "87f68831-d1ea-493c-8b94-e61fcd1c4a08";
public static PublicClientApplication PublicClientApp { get; } = new PublicClientApplication(ClientId);