1- using System ;
2- using System . Collections . Generic ;
3- using System . Security . Claims ;
4- using System . Threading . Tasks ;
5- using eFormAPI . Web . Infrastructure . Data . Entities ;
6- using eFormAPI . Web . Infrastructure . Identity ;
7- using Microsoft . AspNet . Identity . Owin ;
8- using Microsoft . Owin . Security ;
9- using Microsoft . Owin . Security . Cookies ;
10- using Microsoft . Owin . Security . OAuth ;
11-
12- namespace eFormAPI . Web . Infrastructure . Security
13- {
14- public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider
15- {
16- private readonly string _publicClientId ;
17-
18- public ApplicationOAuthProvider ( string publicClientId )
19- {
20- _publicClientId = publicClientId ?? throw new ArgumentNullException ( $ "publicClientId") ;
21- }
22-
23- public override async Task GrantResourceOwnerCredentials ( OAuthGrantResourceOwnerCredentialsContext context )
24- {
25- var userManager = context . OwinContext . GetUserManager < EformUserManager > ( ) ;
26-
27- EformUser user = await userManager . FindAsync ( context . UserName , context . Password ) ;
28-
29- if ( user == null )
30- {
31- context . SetError ( "The user name or password is incorrect." , "The user name or password is incorrect." ) ;
32- return ;
33- }
34-
35- ClaimsIdentity oAuthIdentity = await user . GenerateUserIdentityAsync ( userManager ,
36- OAuthDefaults . AuthenticationType ) ;
37- ClaimsIdentity cookiesIdentity = await user . GenerateUserIdentityAsync ( userManager ,
38- CookieAuthenticationDefaults . AuthenticationType ) ;
39-
40- AuthenticationProperties properties = CreateProperties ( user . UserName ) ;
41- AuthenticationTicket ticket = new AuthenticationTicket ( oAuthIdentity , properties ) ;
42- context . Validated ( ticket ) ;
43- context . Request . Context . Authentication . SignIn ( cookiesIdentity ) ;
44- }
45-
46- public override Task TokenEndpoint ( OAuthTokenEndpointContext context )
47- {
48- foreach ( KeyValuePair < string , string > property in context . Properties . Dictionary )
49- {
50- context . AdditionalResponseParameters . Add ( property . Key , property . Value ) ;
51- }
52-
53- return Task . FromResult < object > ( null ) ;
54- }
55-
56- public override Task ValidateClientAuthentication ( OAuthValidateClientAuthenticationContext context )
57- {
58- // Resource owner password credentials does not provide a client ID.
59- if ( context . ClientId == null )
60- {
61- context . Validated ( ) ;
62- }
63-
64- return Task . FromResult < object > ( null ) ;
65- }
66-
67- public override Task ValidateClientRedirectUri ( OAuthValidateClientRedirectUriContext context )
68- {
69- if ( context . ClientId == _publicClientId )
70- {
71- Uri expectedRootUri = new Uri ( context . Request . Uri , "/" ) ;
72-
73- if ( expectedRootUri . AbsoluteUri == context . RedirectUri )
74- {
75- context . Validated ( ) ;
76- }
77- }
78-
79- return Task . FromResult < object > ( null ) ;
80- }
81-
82- public static AuthenticationProperties CreateProperties ( string userName )
83- {
84- IDictionary < string , string > data = new Dictionary < string , string >
85- {
86- { "userName" , userName }
87- } ;
88- return new AuthenticationProperties ( data ) ;
89- }
90- }
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Security . Claims ;
4+ using System . Threading . Tasks ;
5+ using eFormAPI . Web . Infrastructure . Data . Entities ;
6+ using eFormAPI . Web . Infrastructure . Identity ;
7+ using Microsoft . AspNet . Identity . Owin ;
8+ using Microsoft . Owin . Security ;
9+ using Microsoft . Owin . Security . Cookies ;
10+ using Microsoft . Owin . Security . OAuth ;
11+
12+ namespace eFormAPI . Web . Infrastructure . Security
13+ {
14+ public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider
15+ {
16+ private readonly string _publicClientId ;
17+
18+ public ApplicationOAuthProvider ( string publicClientId )
19+ {
20+ _publicClientId = publicClientId ?? throw new ArgumentNullException ( $ "publicClientId") ;
21+ }
22+
23+ public override async Task GrantResourceOwnerCredentials ( OAuthGrantResourceOwnerCredentialsContext context )
24+ {
25+
26+ var userManager = context . OwinContext . GetUserManager < EformUserManager > ( ) ;
27+
28+ EformUser user = await userManager . FindAsync ( context . UserName , context . Password ) ;
29+
30+ if ( user == null )
31+ {
32+ context . SetError ( "The user name or password is incorrect." , "The user name or password is incorrect." ) ;
33+ return ;
34+ }
35+
36+ ClaimsIdentity oAuthIdentity = await user . GenerateUserIdentityAsync ( userManager ,
37+ OAuthDefaults . AuthenticationType ) ;
38+ ClaimsIdentity cookiesIdentity = await user . GenerateUserIdentityAsync ( userManager ,
39+ CookieAuthenticationDefaults . AuthenticationType ) ;
40+
41+ AuthenticationProperties properties = CreateProperties ( user . UserName ) ;
42+ AuthenticationTicket ticket = new AuthenticationTicket ( oAuthIdentity , properties ) ;
43+ context . Validated ( ticket ) ;
44+ context . Request . Context . Authentication . SignIn ( cookiesIdentity ) ;
45+ }
46+
47+ public override Task TokenEndpoint ( OAuthTokenEndpointContext context )
48+ {
49+ foreach ( KeyValuePair < string , string > property in context . Properties . Dictionary )
50+ {
51+ context . AdditionalResponseParameters . Add ( property . Key , property . Value ) ;
52+ }
53+
54+ return Task . FromResult < object > ( null ) ;
55+ }
56+
57+ public override Task ValidateClientAuthentication ( OAuthValidateClientAuthenticationContext context )
58+ {
59+ // Resource owner password credentials does not provide a client ID.
60+ if ( context . ClientId == null )
61+ {
62+ context . Validated ( ) ;
63+ }
64+
65+ return Task . FromResult < object > ( null ) ;
66+ }
67+
68+ public override Task ValidateClientRedirectUri ( OAuthValidateClientRedirectUriContext context )
69+ {
70+ if ( context . ClientId == _publicClientId )
71+ {
72+ Uri expectedRootUri = new Uri ( context . Request . Uri , "/" ) ;
73+
74+ if ( expectedRootUri . AbsoluteUri == context . RedirectUri )
75+ {
76+ context . Validated ( ) ;
77+ }
78+ }
79+
80+ return Task . FromResult < object > ( null ) ;
81+ }
82+
83+ public static AuthenticationProperties CreateProperties ( string userName )
84+ {
85+ IDictionary < string , string > data = new Dictionary < string , string >
86+ {
87+ { "userName" , userName }
88+ } ;
89+ return new AuthenticationProperties ( data ) ;
90+ }
91+ }
9192}
0 commit comments