简体   繁体   中英

Read Azure AD login user details in ASP.net

I'm implementing Azure AD login in asp.net (VS 2013)..
I have successfully created sign-in in and sign-out parts..

My sign -in and sign-out methods are like below.

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);

    }

But my problem is I can't assign logon user details like username ,Name to asp:Label ..

I have tried

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();

        }

    }

This is very important to me.. anyone can help me..

There is a sample in the tutorial: https://azure.microsoft.com/en-us/documentation/articles/active-directory-devquickstarts-webapp-dotnet/ .

You could see the method to get user information in the 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM