Skip to content

Commit 94ddf22

Browse files
committed
WebAPI Authorization DB added
1 parent 310b620 commit 94ddf22

File tree

154 files changed

+4424
-1485
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+4424
-1485
lines changed

eFormAPI/eFormAPI/App_Start/AutofacConfig.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@ public static class AutofacConfig
1212
public static void ConfigureContainer()
1313
{
1414
var builder = new ContainerBuilder();
15-
1615
// Get your HttpConfiguration.
1716
var config = GlobalConfiguration.Configuration;
18-
1917
// Register your Web API controllers.
2018
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
21-
2219
// OPTIONAL: Register the Autofac filter provider.
2320
builder.RegisterWebApiFilterProvider(config);
24-
2521
// Set the dependency resolver to be Autofac.
2622
Container = builder.Build();
2723
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using eFormAPI.Web.Infrastructure.Data;
3+
using eFormAPI.Web.Infrastructure.Identity;
4+
using eFormAPI.Web.Infrastructure.Security;
5+
using Microsoft.AspNet.Identity;
6+
using Microsoft.Owin;
7+
using Microsoft.Owin.Security.Cookies;
8+
using Microsoft.Owin.Security.OAuth;
9+
using Owin;
10+
11+
namespace eFormAPI.Web
12+
{
13+
public partial class Startup
14+
{
15+
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
16+
17+
public static string PublicClientId { get; private set; }
18+
19+
// For more information on configuring authentication, please visit https://go.microsoft.com/fwlink/?LinkId=301864
20+
public void ConfigureAuth(IAppBuilder app)
21+
{
22+
// Configure the db context and user manager to use a single instance per request
23+
app.CreatePerOwinContext(BaseDbContext.Create);
24+
app.CreatePerOwinContext<EformUserManager>(EformUserManager.Create);
25+
26+
// Enable the application to use a cookie to store information for the signed in user
27+
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
28+
app.UseCookieAuthentication(new CookieAuthenticationOptions());
29+
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
30+
31+
// Configure the application for OAuth based flow
32+
PublicClientId = "self";
33+
OAuthOptions = new OAuthAuthorizationServerOptions
34+
{
35+
TokenEndpointPath = new PathString("/api/auth/token"),
36+
Provider = new ApplicationOAuthProvider(PublicClientId),
37+
AuthorizeEndpointPath = new PathString("/api/auth/external-login"),
38+
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
39+
// In production mode set AllowInsecureHttp = false
40+
AllowInsecureHttp = true
41+
};
42+
43+
// Enable the application to use bearer tokens to authenticate users
44+
app.UseOAuthBearerTokens(OAuthOptions);
45+
46+
// Uncomment the following lines to enable logging in with third party login providers
47+
//app.UseMicrosoftAccountAuthentication(
48+
// clientId: "",
49+
// clientSecret: "");
50+
51+
//app.UseTwitterAuthentication(
52+
// consumerKey: "",
53+
// consumerSecret: "");
54+
55+
//app.UseFacebookAuthentication(
56+
// appId: "",
57+
// appSecret: "");
58+
59+
//app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
60+
//{
61+
// ClientId = "",
62+
// ClientSecret = ""
63+
//});
64+
}
65+
}
66+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Web.Http;
4+
using System.Web.Http.Description;
5+
using Swashbuckle.Application;
6+
using Swashbuckle.Swagger;
7+
8+
namespace eFormAPI.Web
9+
{
10+
public class SwaggerConfig
11+
{
12+
public static void Register(HttpConfiguration _configuration)
13+
{
14+
_configuration.EnableSwagger(c =>
15+
{
16+
c.SingleApiVersion("v1", "Eform API");
17+
c.IncludeXmlComments($@"{System.AppDomain.CurrentDomain.BaseDirectory}\bin\API.docs.xml");
18+
c.DescribeAllEnumsAsStrings();
19+
c.DocumentFilter<AuthTokenOperation>();
20+
c.OperationFilter<AddAuthorizationHeader>();
21+
}).EnableSwaggerUi();
22+
}
23+
}
24+
25+
public class AuthTokenOperation : IDocumentFilter
26+
{
27+
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
28+
{
29+
swaggerDoc.paths.Add("/api/auth/token", new PathItem
30+
{
31+
post = new Operation
32+
{
33+
tags = new List<string> { "Auth" },
34+
consumes = new List<string>
35+
{
36+
"application/x-www-form-urlencoded"
37+
},
38+
parameters = new List<Parameter> {
39+
new Parameter
40+
{
41+
type = "string",
42+
name = "grant_type",
43+
required = true,
44+
@in = "formData",
45+
@default = "password"
46+
},
47+
new Parameter
48+
{
49+
type = "string",
50+
name = "username",
51+
required = false,
52+
@in = "formData"
53+
},
54+
new Parameter
55+
{
56+
type = "string",
57+
name = "password",
58+
required = false,
59+
@in = "formData"
60+
}
61+
}
62+
}
63+
});
64+
}
65+
}
66+
67+
public class AddAuthorizationHeader : IOperationFilter
68+
{
69+
/// <summary>
70+
/// Adds an authorization header to the given operation in Swagger.
71+
/// </summary>
72+
/// <param name="operation">The Swashbuckle operation.</param>
73+
/// <param name="schemaRegistry">The Swashbuckle schema registry.</param>
74+
/// <param name="apiDescription">The Swashbuckle api description.</param>
75+
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
76+
{
77+
if (operation == null) return;
78+
79+
if (operation.parameters == null)
80+
{
81+
operation.parameters = new List<Parameter>();
82+
}
83+
84+
var parameter = new Parameter
85+
{
86+
description = "The authorization token",
87+
@in = "header",
88+
name = "Authorization",
89+
required = true,
90+
type = "string",
91+
@default = "Bearer "
92+
};
93+
94+
if (apiDescription.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any())
95+
{
96+
parameter.required = false;
97+
}
98+
99+
operation.parameters.Add(parameter);
100+
}
101+
}
102+
}

eFormAPI/eFormAPI/App_Start/WebApiConfig.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Net.Http.Headers;
44
using System.Web.Http;
55
using Autofac.Integration.WebApi;
6+
using Microsoft.Owin.Security.OAuth;
67
using Newtonsoft.Json;
78
using Newtonsoft.Json.Serialization;
89

@@ -12,6 +13,11 @@ public static class WebApiConfig
1213
{
1314
public static void Register(HttpConfiguration config)
1415
{
16+
// Web API configuration and services
17+
// Configure Web API to use only bearer token authentication.
18+
config.SuppressDefaultHostAuthentication();
19+
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
20+
1521
config.MapHttpAttributeRoutes();
1622
var container = AutofacConfig.Container;
1723
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

eFormAPI/eFormAPI/Areas/HelpPage/ModelDescriptions/ComplexTypeModelDescription.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)