阅读ASP.net中的Azure AD登录用户详细信息 [英] Read Azure AD login user details in ASP.net

查看:79
本文介绍了阅读ASP.net中的Azure AD登录用户详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的登录和注销方法如下.

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

    }

但是我的问题是我无法将登录用户详细信息(如username,Name)分配给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();

        }

    }

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

解决方案

教程中有一个示例:

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

这篇关于阅读ASP.net中的Azure AD登录用户详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆