Batuhan Avlayan
Batuhan Avlayan

Reputation: 421

AuthenticationProperties not working in Token based authentication

I use OAuth Bearer token based authentication in my project. After a successful login request, I recieve the following json.

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

I want to send more information data in below json. I created authentication ticket and added authenticationproperties. But it isn't not working.

GrantResourceOwnerCredentials method code:

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 method code:

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

Upvotes: 2

Views: 2444

Answers (2)

Nayas Subramanian
Nayas Subramanian

Reputation: 2379

It works..

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

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

Upvotes: 1

habib
habib

Reputation: 141

Override TokenEndpoint method.

    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);
    }

Upvotes: 9

Related Questions