Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the logged user with an OWIN authentication?

The login code in my AccountController

var claims = new List<Claim>();  
claims.Add(new Claim(ClaimTypes.Name, user.Name+" "+user.Surname));
claims.Add(new Claim(ClaimTypes.Role, role.Code));
var claimIdenties = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
var ctx = Request.GetOwinContext();
var authenticationManager = ctx.Authentication;
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, claimIdenties);

In my PrivateController I want to access to the Id of the authenticated user, I'm searching an ID field or something like that but

enter image description here

How to get the logged user with an OWIN authentication?

Edit

Request.GetOwinContext().Authentication.User.Identity

Gives me access to the following propertys:

  • AuthenticationType
  • IsAuthenticated
  • Name //Claim.Name

How can I set an ID like a 'Claim.ID' and retrieve it?

Second edit

This post https://stackoverflow.com/a/24893167/4470880 solved my problem.

like image 274
flofreelance Avatar asked Oct 24 '25 14:10

flofreelance


1 Answers

Try this alternative:

var user = HttpContext.GetOwinContext().Authentication.User;

For getting name of the `user`:
string name = HttpContext.GetOwinContext().Authentication.User.Identity.Name;
like image 92
Murat Yıldız Avatar answered Oct 26 '25 04:10

Murat Yıldız



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!