Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Customer
- {
- public int Id { get; set; }
- public string Username { get; set; }
- public string Password { get; set; }
- public string Role{ get; set; }
- }
- public class MyAuthorizationServerProvider : OAuthAuthorizationServerProvider
- {
- public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
- {
- context.Validated(); //
- }
- public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
- {
- var identity = new ClaimsIdentity(context.Options.AuthenticationType);
- if (context.UserName == "user" && context.Password == "pass")
- {
- identity.AddClaim(new Claim(ClaimTypes.Role, "user"));
- identity.AddClaim(new Claim("username", "user"));
- identity.AddClaim(new Claim(ClaimTypes.Name, "Suresh Sha"));
- context.Validated(identity);
- }
- else
- {
- context.SetError("invalid_grant", "Provided username and password is incorrect");
- return;
- }
- }
- }
- if (context.UserName == "user" && context.Password == "pass")
- {
- identity.AddClaim(new Claim(ClaimTypes.Role, "user"));
- identity.AddClaim(new Claim("username", "user"));
- identity.AddClaim(new Claim(ClaimTypes.Name, "NAME"));
- context.Validated(identity);
- }
- public partial class Startup
- {
- public void Configuration(IAppBuilder app)
- {
- //enable cors origin requests
- app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
- var myProvider = new MyAuthorizationServerProvider();
- OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions
- {
- AllowInsecureHttp = true,
- TokenEndpointPath = new PathString("/token"),
- AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
- Provider = myProvider
- };
- app.UseOAuthAuthorizationServer(options);
- app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
- //my code
- HttpConfiguration config = new HttpConfiguration();
- WebApiConfig.Register(config);
- //ConfigureAuth(app);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement