AuthenticationProperties无法在基于令牌的身份验证中工作

时间:2016-02-24 22:13:41

标签: oauth asp.net-web-api2 owin

我在项目中使用基于OAuth Bearer令牌的身份验证。成功登录请求后,我收到以下json。

{"access_token":"some token","token_type":"bearer","expires_in":1232}

我想在json下面发送更多信息数据。我创建了身份验证票证并添加了authenticationproperties。但它并没有起作用。

GrantResourceOwnerCredentials方法代码:

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        try
        {
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
            var schoolId = context.UserName;
            var password = context.Password;
            logger.InfoFormat(CommonConstants.LoginInfoLogMessageFormat, schoolId);
            var loginOperator = new LoginManager();
            var result = loginOperator.IsUser(schoolId, password);
            if (result)
            {
                var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                identity.AddClaim(new Claim("sub", context.UserName));
                identity.AddClaim(new Claim("role", "user"));
                var authenticationProperties = GetUserAuthenticationProperties();
                var authenticationTicket = new AuthenticationTicket(identity, authenticationProperties);
                context.Validated(authenticationTicket);
            }
            else
            {
                context.SetError("invalid_grant", "Kullanıcı adı veya şifre yanlış.");
            }
        }
        catch (Exception exception)
        {
            logger.ErrorFormat("An error occured GrantResourceOwnerCredentials() method: {0}", exception);
        }

    }

GetUserAuthenticationProperties方法代码:

private AuthenticationProperties GetUserAuthenticationProperties()
    {
        IDictionary<string, string> authenticationInformation = new Dictionary<string, string>();
        authenticationInformation.Add("batuhan", "avlayan");
        authenticationInformation.Add("fuat", "bugra");
        return new AuthenticationProperties(authenticationInformation);
    }

2 个答案:

答案 0 :(得分:6)

覆盖TokenEndpoint方法。

    public override Task TokenEndpoint(OAuthTokenEndpointContext context)
    {
        foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
        {
            context.AdditionalResponseParameters.Add(property.Key, property.Value);
        }
        return Task.FromResult<object>(null);
    }

答案 1 :(得分:0)

有效..

   public override Task TokenEndpoint(OAuthTokenEndpointContext context)
    {
        context.AdditionalResponseParameters.Add("displayname", displayName);

        return Task.FromResult<object>(null);
    }