小编Sha*_*kul的帖子

带有MVC 4.0的DotNetOpenAuth

我一直在尝试使用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)

asp.net asp.net-mvc dotnetopenauth asp.net-mvc-4

7
推荐指数
1
解决办法
2981
查看次数

通过Postman将承载令牌发送到Web API

更新

我已经能够使用此线程的指令获得Bearer令牌

以下是邮递员的说明:

  • 网址:https://login.windows.net/ [ localname] .onmicrosoft.com/oauth2/ token
  • 键入:POST
  • 标题:没有
  • 正文:表单数据
  • grant_type:client_credentials
  • client_id:[client-id]
  • client_secret:[client-secret]

邮差中的承载令牌示例

但是,如果我在调用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)

使用Bearer Token调用Postman的示例

jwt asp.net-web-api azure-ad-graph-api

7
推荐指数
1
解决办法
5438
查看次数