【问题标题】:Always getting the "Authorization has been denied for this request." message总是收到“此请求的授权已被拒绝”。信息
【发布时间】:2015-05-22 05:36:42
【问题描述】:

我能够成功检索令牌,但是在尝试使用令牌进行身份验证时,我总是收到 Authorization has been denied for this request 消息。

我的Startup.cs 文件包含以下方法

public void Configuration(IAppBuilder app)
{
    var config = new HttpConfiguration();
    WebApiConfig.Register(config);

    app.UseWebApi(config);

    ConfigureOAuth(app);

    var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter().First();
    jsonFormatter.SerializerSettings
                 .ContractResolver = new CamelCasePropertyNamesContractResolver();
}

private void ConfigureOAuth(IAppBuilder app)
{
    var oAuthServerOptions = new OAuthAuthorizationServerOptions()
    {
        AllowInsecureHttp = true,
        TokenEndpointPath = new PathString("/Token"),
        AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
        Provider = new DefaultAuthorizationServerProvider()
    };

    app.UseOAuthAuthorizationServer(oAuthServerOptions);
    app.UseOAuthBearerAuthentication(new   OAuthBearerAuthenticationOptions());
}

DefaultAuthorizationServerProvider.cs 类包含以下内容

public class DefaultAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
    public override async Task ValidateClientAuthentication
        (
        OAuthValidateClientAuthenticationContext context
        )
    {
        context.Validated();
    }

    public override async Task GrantResourceOwnerCredentials
        (
        OAuthGrantResourceOwnerCredentialsContext context
        )
    {
        context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

        var identityManager = new IdentityManager();

        var identity = identityManager.Get(context.UserName, context.Password,
            new IpAddressProvider().Provide(IpAddressType.Forwarding));

        if (identity == null)
        {
            context.SetError("invalid_grant", "Authentication failed. Please make sure you provided the correct username and password.");
        }
        else
        {
            identity.AddClaim(new Claim(ClaimTypes.Role, "User"));
            context.Validated(identity);
        }
    }
}

IdentityManager.cs 类有以下内容

public class IdentityManager : IIdentityManager
{
    public virtual ClaimsIdentity Get
       (
       string username,
       string password,
       string ipAddress
       )
    {
        var authenticateUserWorkflowOutput = new AuthenticateUserWorkflowHelper().Execute
            (
                new AuthenticateUserWorkflowInput
                {
                    Username = username,
                    Password = password,
                    IpAddress = ipAddress
                },
                new AuthenticateUserWorkflowState()
            );

        if (authenticateUserWorkflowOutput.Message.Exception != null)
        {
            return null;
        }

        if (!authenticateUserWorkflowOutput.Authenticated)
        {
            return null;
        }

        return authenticateUserWorkflowOutput.User != null ? new Infrastructure.Identity(new[]
        {
            new Claim(ClaimTypes.Name, authenticateUserWorkflowOutput.MasterUser.EmailAddress), 
        }, "ApplicationCookie") : null;
    }
}

使用 Fiddler 我可以成功检索令牌

但是当我尝试使用令牌进行身份验证时,我得到以下响应

【问题讨论】:

    标签: c# token asp.net-web-api owin


    【解决方案1】:

    好的,我在 Startup 课程中发现了问题。我错过了以下内容

    [assembly: OwinStartup(typeof(Yugasat.System.ServiceLayer.Startup))]
    namespace Yugasat.System.ServiceLayer
    

    并且需要将ConfigureOAuth(app);call 移动到Configuration 方法的顶部。下面是我的新Startup.cs 课程。

    [assembly: OwinStartup(typeof(Yugasat.System.ServiceLayer.Startup))]
    namespace Yugasat.System.ServiceLayer
    {
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                ConfigureOAuth(app);
    
                var config = new HttpConfiguration();
                WebApiConfig.Register(config);
    
                app.UseWebApi(config);
    
                var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
                jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            }
    
            private void ConfigureOAuth(IAppBuilder app)
            {
                var oAuthServerOptions = new OAuthAuthorizationServerOptions()
                {
                    AllowInsecureHttp = true,
                    TokenEndpointPath = new PathString("/Token"),
                    AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
                    Provider = new DefaultAuthorizationServerProvider()
                };
    
                app.UseOAuthAuthorizationServer(oAuthServerOptions);
                app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 2019-07-17
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 2017-11-07
      相关资源
      最近更新 更多