簡體   English   中英

始終獲得“此請求已被拒絕授權。”消息

[英]Always getting the “Authorization has been denied for this request.” message

我能夠成功檢索令牌,但是當嘗試使用令牌進行身份驗證時,我總是得到Authorization has been denied for this request消息的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我可以成功檢索令牌

在此輸入圖像描述

但是當我嘗試使用令牌進行身份驗證時,我得到以下響應

在此輸入圖像描述

好的,我在Startup課程中發現了這個問題。 我錯過了以下內容

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

ConfigureOAuth(app); 需要將調用移動到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());
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM