Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

expect an example about oauth #76

Closed
seven1986 opened this issue Nov 2, 2016 · 3 comments
Closed

expect an example about oauth #76

seven1986 opened this issue Nov 2, 2016 · 3 comments

Comments

@seven1986
Copy link

seven1986 commented Nov 2, 2016

Thanks a lot
When client got accessToken they do need appkey with any request, So how can I limit the request before valiate accessToken?
I am going to use it in my product envirment, so could you help me? thanks

@seven1986 seven1986 changed the title expect a example about oauth expect an example about oauth Nov 8, 2016
@stefanprodan
Copy link
Owner

stefanprodan commented Nov 12, 2016

When a user is authorize, you'll have to add a claim to the identity store, like userId or whatever you use to uniquely identify your clients. Then you can override the ThrottlingMiddleware.SetIdentity function and use that claim as the throttle key. Something like this:

public class OAuthThrottlingMiddleware : ThrottlingMiddleware
{
    protected override RequestIdentity SetIdentity(IOwinRequest request)
    {
          var userId = "anon";
         if(request.Context.User.Identity.IsAuthenticated)
         {
            //get userId from identity claim 
         }
        return new RequestIdentity()
        {
            ClientKey = userId,
            ClientIp = base.GetClientIp(request).ToString(),
            Endpoint = request.RequestUri.AbsolutePath.ToLowerInvariant()
        };
    }
}

@seven1276
Copy link

Thanks,It works,fantastic!
And must put this code app.Use<OAuthThrottlingMiddleware>(); after app.use OAuth API

One more question I want to know is about
image

SetIdentity does not excute when I write 'base.PolicyRepository = new PolicyMemoryCacheRepository()'

@stefanprodan
Copy link
Owner

stefanprodan commented Nov 13, 2016

Remove that constructor and build your Policy outside the OAuthThrottlingMiddleware:

public class Startup
{
    public void Configuration(IAppBuilder appBuilder)
    {
        var oauthPolicy = BuildPolicyFromDB();

        appBuilder.Use(typeof(OAuthThrottlingMiddleware),
            oauthPolicy,
            new PolicyMemoryCacheRepository(),
            new MemoryCacheRepository(),
            null,
            null);
    }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants