我在Microsoft Azure上创建了一个Web表单项目。 Visual Studio负责为您创建所有Web表单。其中一个名为UserInfo.aspx的Web表单,在其后端有一个函数,用于从Azure上的域控制器查询用户数据,如下所示:
private ApplicationDbContext db = new ApplicationDbContext();
private static string clientId = ConfigurationManager.AppSettings["ida:ClientID"];
private static string appKey = ConfigurationManager.AppSettings["ida:ClientSecret"];
private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
private static string graphResourceId = "https://graph.windows.net";
public Task GetUserData()
{
return Task.Run(() =>
{
string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
try
{
Uri servicePointUri = new Uri(graphResourceId);
Uri serviceRoot = new Uri(servicePointUri, tenantID);
ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot,
async () => await GetTokenForApplication());
// use the token for querying the graph to get the user details
IUser user = activeDirectoryClient.Users
.Where(u => u.ObjectId.Equals(userObjectID))
.ExecuteAsync().Result.CurrentPage.ToList().First();
UserData.DataSource = new List<IUser> { user };
UserData.DataBind();
}
// if the above failed, the user needs to explicitly re-authenticate for the app to obtain the required token
catch (AdalException)
{
GetToken.Visible = true;
}
// if the above failed, the user needs to explicitly re-authenticate for the app to obtain the required token
catch (Exception)
{
ShowData.Visible = false;
GetToken.Visible = true;
}
});
}
public async Task<string> GetTokenForApplication()
{
string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
// get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc)
ClientCredential clientcred = new ClientCredential(clientId, appKey);
// initialize AuthenticationContext with the token cache of the currently signed in user, as kept in the app's EF DB
AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID, new ADALTokenCache(signedInUserID));
AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenSilentAsync(graphResourceId, clientcred, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
return authenticationResult.AccessToken;
}
正如您在GetUserData函数中注意到的那样,Visual Studio将用户信息保存在变量&#39; user&#39;具有IUser类型。
&#39;用户&#39;变量具有我想要的属性,例如&#39; JobTitle&#39;或者&#39; city&#39;。我希望能够在我的应用程序开始时访问此用户&#39;变量然后在我的项目中随处使用它。
我尝试创建一个返回IUser的函数,而不是&#39;任务&#39;但它不起作用。我的意图是使用&#39; user&#39;变量以防止用户通过选中“JobTitle”来访问某些页面。我也想使用属性&#39; JobTitle&#39;在我的javascript文件(前端)中做额外的验证。
提前谢谢!
答案 0 :(得分:0)
如果我们想要返回用户信息,请尝试将功能返回类型从任务更改为Task <IUser>
或Task<List<IUser>>
public async Task<IUser> GetUserData()
{
... // logic
return user;
}