手动设置cookie使用accesstoken / AuthenticationResult -like UseCookieAuthentication()owin中间件

时间:2015-03-18 11:45:33

标签: c# asp.net-mvc owin

在Controller中的Action中,我们有AuthenticationResult,其中包含AccessToken。

我们希望使用此令牌进行手动登录,该令牌与中间件中的UserCookieAuthentication相同。这可能吗?

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseWindowsAzureActiveDirectoryBearerAuthentication(
            new WindowsAzureActiveDirectoryBearerAuthenticationOptions
            {
                Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
                TokenValidationParameters = new TokenValidationParameters
                {
                ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
            },
            });
    }
}

我一直在查看HttpContext.GetOwinContext()。Authentication.SignIn()但是它没有任何令牌。

1 个答案:

答案 0 :(得分:2)

在Katana / Owin Github项目中,以下TokenHelper使用AuthenticationResponseGrant方法解决了这个问题:

                result = await authContext.AcquireTokenAsync(resource, clientId, uc);

                var principal = await new TokenHelper().GetValidatedClaimsPrincipalAsync(result.AccessToken);

                var claimsIdentity = new ClaimsIdentity(principal.Claims, CookieAuthenticationDefaults.AuthenticationType);

                var properties = new AuthenticationProperties();

                HttpContext.GetOwinContext().Authentication.AuthenticationResponseGrant =
                    new AuthenticationResponseGrant(claimsIdentity, properties);