简体   繁体   English

ASP MVC模型或存储库中的Owin上下文

[英]Owin Context in ASP MVC model or repository

Im trying am using Owin without identity. 我正在尝试使用没有身份的Owin。 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. 在启动类中,我在提供程序的OnResponseSigned中进行设置,将Thread.CurrentPrinipal Context.OwinContext.Authentcation设置为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. 不知道我应该使用Owin上下文还是可以使用Thread.current上下文。 When I inspect Thread.CurrentPrincipal it seems to be there just not in claims. 当我检查Thread.CurrentPrincipal时,似乎没有出现在声明中。 Any thoughts? 有什么想法吗?

This is a work around but to fix this I went and got the owin context instead of using the Thread.CurrentPrincipal. 这是一个变通办法,但要解决此问题,我去了并获得了owin上下文,而不是使用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. 仍然不确定为什么Thread.CurrentPrincipal不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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