我将WebApi2添加到我的MVC应用程序中,我可以通过浏览器成功调用我的API . 如果用户未经过身份验证,则会显示我的标准登录屏幕然后运行 .
但我真的想把api称为来自移动应用程序的REST api . 我将以下代码添加到搜索时找到的启动中 . 但我不知道如何通过网址实际登录,或者在我的通话中传递和使用令牌 .
我试过例如myurl / api / Account / ExternalLogin,但是我得到了无效的请求 .
PublicClientId = "self";
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
// Note: Remove the following line before you deploy to production:
AllowInsecureHttp = true
};
所以问题是,如何实际使用REST api调用,或者我是否需要在控制器中添加其他代码 .
1 回答
将API配置为使用OAuth后,您可以使用以下代码获取访问令牌
一旦有了访问令牌,就可以使用类似下面的内容将访问令牌传递给API调用