gpt4 book ai didi

asp.net - 在 ASP.net 中读取 Azure AD 登录用户详细信息

转载 作者:行者123 更新时间:2023-12-05 08:33:53 29 4
gpt4 key购买 nike

我正在 asp.net 中实现 Azure AD 登录(VS 2013)..
我已成功创建登录和注销部分..

我的登录和退出方法如下。

public partial class Startup
{

private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];

private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];

private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"];

private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];

string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);

public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

app.UseCookieAuthentication(new CookieAuthenticationOptions());

app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = context =>
{
context.HandleResponse();
context.Response.Redirect("/Error?message=" + context.Exception.Message);
return Task.FromResult(0);
}
}
});
}
}

public void SignIn()
{

if (!Request.IsAuthenticated)
{ HttpContext.Current.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
}

public void SignOut()
{
HttpContext.Current.GetOwinContext().Authentication.SignOut(
OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);

}

但我的问题是我无法将登录用户详细信息(例如用户名、名称)分配给 asp:Label ..

我已经尝试过

protected void Page_Load(object sender, EventArgs e)

{
if (!Request.IsAuthenticated)
{
string UserName = HttpContext.Current.GetOwinContext().Authentication.User.Identity.Name.ToString();
}

if (Request.IsAuthenticated)
{

string UserName = HttpContext.Current.GetOwinContext().Authentication.User.Identity.Name.ToString();

}

}

这对我来说非常重要..任何人都可以帮助我..

最佳答案

教程中有一个示例:https://azure.microsoft.com/en-us/documentation/articles/active-directory-devquickstarts-webapp-dotnet/ .

在Step4中可以看到获取用户信息的方法:

    public ActionResult About()
{
ViewBag.Name = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Name).Value;
ViewBag.ObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
ViewBag.GivenName = ClaimsPrincipal.Current.FindFirst(ClaimTypes.GivenName).Value;
ViewBag.Surname = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Surname).Value;
// ViewBag.UPN = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn).Value;

return View();
}

关于asp.net - 在 ASP.net 中读取 Azure AD 登录用户详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35750295/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com