简体   繁体   中英

get access token via asp.net Microsoft.Owin.Security.OAuth

According to below references..,

I got below code in order to create access_token.

var ticket = new AuthenticationTicket(identity, props);
OAuthGrantResourceOwnerCredentialsContext.Validated(ticket);

But unfortunately, this Validated(ticket) method does not return the access_token which is generated auto-magically.

What I want to get is like below code.

string _access_token = OAuthGrantResourceOwnerCredentialsContext.Validated(ticket);
System.out.println("Access_token ="+_access_token);

Please let me get your suggestion.

You must override method TokenEndpointResponse of class OAuthAuthorizationServerProvider and get the property AccessToken of context.

public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider
{
    public override Task TokenEndpointResponse(OAuthTokenEndpointResponseContext context)
    {
        var token = context.AccessToken;
        return base.TokenEndpointResponse(context);
    }
}

And of course, you must call your custom provider

static Startup()
{
    OAuthOptions = new OAuthAuthorizationServerOptions
    {
        TokenEndpointPath = new PathString("/Token"),
        Provider = new ApplicationOAuthProvider(),
        AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(60),
        AllowInsecureHttp = true,
        RefreshTokenProvider = new ApplicationRefreshTokenProvider(),
    };
}

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