Click here to Skip to main content
14,241,236 members
Rate this:
Please Sign up or sign in to vote.
This is my statrup Class

public partial class Startup
   {
       public void Configuration(IAppBuilder app)
       {
           HttpConfiguration config = new HttpConfiguration();
           ConfigureOAuth(app);
           WebApiConfig.Register(config);
           app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
           app.UseWebApi(config);
       }

       //private void ConfigureAuth(IAppBuilder app)
       //{
       //    throw new NotImplementedException();
       //}

       public 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());

       }
   }

This one is my AuthRepositry class
public class AuthRepository : IDisposable
   {
       private MobileAppEntities _ctx;
       private UserManager<ApplicationUser> _userManager;// UserManager { get { return _userManager; } private set { } }
       public AuthRepository()
       {
           _ctx = new MobileAppEntities();
           _userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(_ctx));
       }
       public async Task<IdentityUser> FindUser(string userName, string password)
       {
           IdentityUser user = await _userManager.FindAsync(userName, password);
           return user;
       }
       public void Dispose()
       {
           _ctx.Dispose();
           _userManager.Dispose();
       }
   }


This one Authorization Server Provider Class
public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
   {
       private ApplicationUserManager _userManager;
       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);

       }
   }


What I have tried:

Every time when i am posting request from postman it gave error like that
Error
{
"error": "unsupported_grant_type"
}
Posted
Updated 10-Dec-18 17:17pm
v2
Comments
Vincent Maverick Durano 11-Dec-18 7:20am
   
Can you show us your CURL you used in Postman?
Aitzaz Ahsan 11-Dec-18 7:34am
   
you can see my postman request through link
https://ibb.co/pz9ZZ0k
Richard Deeming 11-Dec-18 8:42am
   
The error in your screen-shot doesn't match the error in your question.
Vincent Maverick Durano 11-Dec-18 19:50pm
   
As already mentioned, the error was different from the original question. For the error in the link, It seems like your user object is null. Check the username and password by debugging your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100