Oauth Authentication doesn’t work in Azure Function

Asked

Viewed 64 times

1

I created a webapi selfhost and used Oauth to generate an authentication token, it worked as a console application but when I try to create a function in Azure it only return server error(500)

My Startup.Cs

    public void Configuration(IAppBuilder app)
    {
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        ConfigureOAuth(app);

        // Configure Web API for self-host. 
        var config = new HttpConfiguration();

        config.DependencyResolver = new ResolveController();
        config.SuppressDefaultHostAuthentication();

        config.Routes.MapHttpRoute(
            name: "ControllersApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        app.UseWebApi(config);
    }

    private void ConfigureOAuth(IAppBuilder app)
    {
        OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new SimpleAuthorizationServerProvider()
        };

        // Token Generation
        app.UseOAuthAuthorizationServer(OAuthServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
    }

Simpleauthorizationserverprovider() (it does not even get here debugging as Azure function)

public class SimpleAuthorizationServerProvider : 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[] { "*" });

        using (AuthRepository _repo = new AuthRepository())
        {
            IdentityUser user = await _repo.FindUser(context.UserName, context.Password);

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

        var identity = new ClaimsIdentity(context.Options.AuthenticationType);
        identity.AddClaim(new Claim("sub", context.UserName));
        identity.AddClaim(new Claim("role", "user"));

        context.Validated(identity);

    }
}
  • 2

    Vc is in Stack Overflow.

  • I’m sorry, corrected it.

  • You need to know what the exception is that is popping. 500 is broad code, it can be anything.

  • Here’s the problem, if I could find where to pop debugging I could solve it. But it’s not even getting to the endpoint

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.