我有一个ASP.NET Web API,我在Azure Active Directory下注册了一个新的应用程序 .
这就是ConfigureAuth在Web API的源代码中的方式 .
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Configure the application for OAuth based flow
OAuthOptions = new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = false,
TokenEndpointPath = new PathString("/Token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
Provider = new ApplicationOAuthProvider("self"),
AuthenticationType = "LocalBearer"
};
// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(OAuthOptions);
}
另一方面,我有一个命令行应用程序,可以访问Web API并向它询问一些数据 .
从命令行我无法使用用户凭据登录(这是一项要求)所以我想生成一个可能在一两年内到期的个人访问令牌,并使用该个人访问令牌构建我的API调用 . 如果可能,那么我可以避免使用用户名/密码登录来生成令牌 .
这个场景有没有文档?如何为我的控制台应用程序生成个人访问令牌以连接到Azure中托管的Web API?
2 回答
AFAIK,Azure AD发出的访问令牌的默认生存期为1h,最大值是在Azure AD中配置令牌生存期后的1天 .
您可以使用OAuth 2.0客户端凭据流从Azure AD获取令牌以访问受保护的API资源,然后您的控制台应用程序将作为应用程序标识运行,而不是作为用户的标识运行 . 请单击here以获取有关守护程序或服务器应用程序到Web API身份验证方案的更多详细信息,并单击here以获取代码示例 . 如果访问令牌过期,ADAL会帮助您续订 .
如果我理解得很好,你就不会这样了,我相信你对 Client Credential flow 很感兴趣,这在下面的例子中有详细解释:
Active-directory-daemon-certificate-credential,这是关于使用证书作为您的应用程序的秘密 . 这个例子的兴趣在于它使用PowerShell脚本完全自动化,以防你想快速尝试
active-directory-dotnet-daemon,与前一个非常相似,但使用密码而不是证书 . 这可能更接近您的需要
在这两种情况下,您都可以表达这些机密何时到期(当您创建证书时,例如您提供到期日期)