我一直在尝试使用ASP.Net MVC 4 Developer Preview的DotNetOpenAuth示例.
我可以从我的测试页面成功调用我的Action,但由于一行代码而遇到了一个奇怪的问题:
var request = _openid.CreateRequest(openIdUrl);
var fetch = new FetchRequest();
fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
fetch.Attributes.AddRequired(WellKnownAttributes.Name.First);
fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last);
request.AddExtension(fetch);
//return RedirectToAction("Login");
return request.RedirectingResponse.AsActionResult(); // <-- This is the line throwing the error
Run Code Online (Sandbox Code Playgroud)
如果我注释掉有问题的代码行并在此之前取消注释,我再也看不到运行时错误了.
到目前为止,我尝试过:
1)确保我有正确的重定向:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
</dependentAssembly>
</assemblyBinding>
<legacyHMACWarning enabled="0" />
</runtime>
Run Code Online (Sandbox Code Playgroud)
2)拥有正确的命名空间:
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange; …Run Code Online (Sandbox Code Playgroud) 更新
我已经能够使用此线程的指令获得Bearer令牌
以下是邮递员的说明:
但是,如果我在调用Web API端点时发送相同的令牌,我仍然会回复" 此请求已拒绝授权 "
为什么还没有授权?
结束更新
我创建了一个ASP.Net Web API项目,该项目使用组织Azure AD实例进行保护.我已正确设置了租户ID,客户端ID和密码.
Azure AD实例与支持Office 365/SharePoint实例的实例相同,其目的是创建可以使用登录用户的上下文调用服务的SharePoint加载项.
我坚持测试API.我可以毫无问题地调用未经授权的端点.但是,当我添加[Authorize]属性时,我总是回复此响应:" 此请求已拒绝授权. "
据我了解,我需要生成一个不记名令牌并将其添加到标题中的Postman请求中(参见图片).经过大量的谷歌搜索,我仍然无法完成这项工作.
我的问题是:如何为受Azure AD保护的Web API实例生成承载令牌.
我的配置代码如下:
public void ConfigureAuth(IAppBuilder app)
{
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
TokenValidationParameters = new TokenValidationParameters {
ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
},
});
}
Run Code Online (Sandbox Code Playgroud)