Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Show username on _layout

$
0
0

I am new to asp.net core. I have moderate experience in C# and WPF. I have some experience with PHP MVC (Phalcon PHP).

I am trying to develop a new application using VS2015 community and host on Azure. I am really new to new type of authentication introduced in ASP.NET core. I do some search on BIng and got the code.

public IActionResult authorise()
        {
            //https://dotnetcodr.com/2013/02/11/introduction-to-claims-based-security-in-net4-5-with-c-part-1/
            //https://www.youtube.com/watch?v=IyFG3XyNq6Q

            if(Request.Form["username"]=="admin" && Request.Form["password"]=="admin")
            {
                var claims = new List<Claim>();
                claims.Add(new Claim(ClaimTypes.Name, Request.Form["username"]));
                claims.Add(new Claim(ClaimTypes.Role, "Admin"));
                var identity = new ClaimsIdentity(claims, AuthenticationTypes.Password);
                var claimsPrincipal = new ClaimsPrincipal(identity);
                // Set current principal
                Thread.CurrentPrincipal = claimsPrincipal;
                HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance", claimsPrincipal);

                GlobalClass.uname = Thread.CurrentPrincipal.Identity.Name.ToString();
                return RedirectToAction("Index","Home");
            }
            return RedirectToAction("Account");
        }

It just a preliminary code and need many optimization. The problem is, I want to put a logout drop down in master page (_Layout.cshtml). I know

Thread.CurrentPrincipal.Identity.Name.ToString();

have the authenticated username. But where to put it to get username on every time I access any page. At present, I will get the usename only after authentication. But the code does not ask to authentication every time I try to open the index (It's a good feature, and I need it).


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>