调试WebApi2登录(/ Token)端点

时间:2014-07-11 08:08:39

标签: oauth asp.net-web-api2

使用.NET WebApi2: 尝试登录WebApi时,我在 / Token时收到500内部服务器错误 端点。

我知道Startup.Auth中有一些配置参数,但无论如何我可以设置断点,或者看看服务器上显示的是什么?(比如,在身份验证检查之前)发生,之后 - 不寻找Fiddler类型的拦截解决方案。)

3 个答案:

答案 0 :(得分:12)

在Providers / ApplicationOAuthProvider.cs中调试 GrantResourceOwnerCredentials 方法

答案 1 :(得分:0)

你可以调试它......但是你需要实现你自己的 ApplicationOAuthProvider ,它应该继承自这个类: OAuthAuthorizationServerProvider

例如:

 public class MyTokenProvider : OAuthAuthorizationServerProvider
{
    private readonly string _publicClientId;

    public MyTokenProvider(string publicClientId)
    {
        if (publicClientId == null)
        {
            throw new ArgumentNullException("publicClientId");
        }

        _publicClientId = publicClientId;
    }

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();

        ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);

        if (user == null)
        {
            context.SetError("invalid_grant", "The user name or password is incorrect.");
            return;
        }

        ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
           OAuthDefaults.AuthenticationType);
        ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
            CookieAuthenticationDefaults.AuthenticationType);

        AuthenticationProperties properties = CreateProperties(user.UserName);
        AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
        context.Validated(ticket);
        context.Request.Context.Authentication.SignIn(cookiesIdentity);
    }

    public override Task TokenEndpoint(OAuthTokenEndpointContext context)
    {
        foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
        {
            context.AdditionalResponseParameters.Add(property.Key, property.Value);
        }

        return Task.FromResult<object>(null);
    }

    public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
        // Resource owner password credentials does not provide a client ID.
        if (context.ClientId == null)
        {
            context.Validated();
        }

        return Task.FromResult<object>(null);
    }

    public override Task ValidateClientRedirectUri(OAuthValidateClientRedirectUriContext context)
    {
        if (context.ClientId == _publicClientId)
        {
            Uri expectedRootUri = new Uri(context.Request.Uri, "/");

            if (expectedRootUri.AbsoluteUri == context.RedirectUri)
            {
                context.Validated();
            }
        }

        return Task.FromResult<object>(null);
    }

    public static AuthenticationProperties CreateProperties(string userName)
    {
        IDictionary<string, string> data = new Dictionary<string, string>
        {
            { "userName", userName }
        };
        return new AuthenticationProperties(data);
    }
}

显然,您需要将其设置为默认提供商,这可以通过 Startup.Auth.cs

完成

示例:

            OAuthOptions = new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/Token"),
            Provider = new MyTokenProvider(PublicClientId),
            AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
            AllowInsecureHttp = true
        };

然后你可以调试它:)

答案 2 :(得分:0)

您可以将Visual Studio配置为中断所有异常,然后在引发错误时获取有关错误的更多信息。

在Visual Studio 2013中,调试菜单,例外,标记与CLR异常相关的两个ckecks,如图所示:

enter image description here