简体   繁体   中英

Owin Context in ASP MVC model or repository

Im trying am using Owin without identity. In my sign in process I am adding claims like so:

[HttpPost]
[AllowAnonymous]
public ActionResult Login(Domain.Model.User.User model, string returnUrl)
{
    var response = someFunctionCall();
    AuthData data = response.authdata;
                IEnumerable<Claim> claim = new List<Claim>(){
                                new Claim(ClaimTypes.CookiePath, MvcApplication.ApplicationPath),
                                new Claim(ClaimTypes.Name, model.UserId),
                                new Claim(Constants.ClaimsConstants.1customClaim, data.1customClaim), 
                                new Claim(Constants.ClaimsConstants.2customClaim, data.2customClaim) 
                };
    ClaimsIdentity id = new ClaimsIdentity(claim, CookieAuthenticationDefaults.AuthenticationType);
    HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties() { IsPersistent = true }, id);
    return RedirectToAction("Index", "Home");
}

In my startup class I am setting in my OnResponseSigned in provider I am setting the Thread.CurrentPrinipal Context.OwinContext.Authentcation as IPrincipal. Like so:

app.UseCookieAuthentication(new CookieAuthenticationOptions()
        {

            CookieSecure = CookieSecureOption.Always,
            LoginPath = new PathString("/Account/Login"),
            AuthenticationMode = AuthenticationMode.Active,
            Provider = new CookieAuthenticationProvider
            {
            OnResponseSignedIn = (context =>
                { 
                  OnSignedInResponse(context);
                })
            )}
        }
private void OnSignedInResponse(CookieResponseSignedInContext context)
    {
       Thread.CurrentPrincipal = context.OwinContext.Authentication as IPrincipal;
    }

later on in the app I need to get acccess to the claims but I can't seem to do that.

    private static readonly ClaimsPrincipal user = Thread.CurrentPrincipal as ClaimsPrincipal;
    private static readonly IEnumerable<Claim> claims = user.Claims;
    public static CustomUserInfo Idenitiy
    {
        get
        {
            UserInfo user = new UserInfo()
            {
                Custom1 = claims.Where(c => c.Type == Constants.ClaimsConstants.1customClaim).First().Value,
                Custom2 = claims.Where(c => c.Type == Constants.ClaimsConstants.2customClaim).First().Value
            };
         return user;
         }
     }

Not sure if I should be using Owin context or if I can use Thread.current context. When I inspect Thread.CurrentPrincipal it seems to be there just not in claims. Any thoughts?

This is a work around but to fix this I went and got the owin context instead of using the Thread.CurrentPrincipal. Like so:

private static readonly ClaimsPrincipal user = System.Web.HttpContext.Current.GetOwinContext().Authentication.User as ClaimsPrincipal;
private static readonly IEnumerable<Claim> claims = user.Claims;
public static CustomUserInfo Idenitiy
{
    get
    {
        UserInfo user = new UserInfo()
        {
            Custom1 = claims.Where(c => c.Type == Constants.ClaimsConstants.1customClaim).First().Value,
            Custom2 = claims.Where(c => c.Type == Constants.ClaimsConstants.2customClaim).First().Value
        };
     return user;
     }
 }

Still not sure why Thread.CurrentPrincipal doesn't work.

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