我正在尝试使用web-api进行令牌教程而且我有点混淆,我无法知道如何获取我的令牌,根据教程,令牌来自Web Api的端点,但我不明白产生令牌的具体功能是什么或如何调用它所以我会得到一个令牌回到GetToken()
,这里有一些代码:
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions()); app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
string PublicClientId = "self";
OAuthAuthorizationServerOptions OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new PureApi.Provider.ApplicationOAuthProvider(PublicClientId),
AuthorizeEndpointPath = new PathString("/api/Login"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
app.UseOAuthBearerTokens(OAuthOptions);
}
static string GetToken(string userName, string password)
{ var pairs = new List<KeyValuePair<string, string>>
{new KeyValuePair<string, string>( "grant_type", "password" ),
new KeyValuePair<string, string>( "username",userName ),
new KeyValuePair<string, string> ( "Password", password ) };
var content = new FormUrlEncodedContent(pairs);
using (var client = new HttpClient())
{
var response = client.PostAsync("http://localhost:2188/api/Test",content).Result;
return response.Content.ReadAsStringAsync().Result;
}
}
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);
}