Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Removed SetupCookies and updated documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
israelramosm committed Aug 30, 2017
1 parent 5f0f0d8 commit 852febd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 54 deletions.
95 changes: 55 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Build Status](https://travis-ci.org/unosquare/swan-aspnetcore.svg?branch=master)](https://travis-ci.org/unosquare/swan-aspnetcore)
[![Build status](https://ci.appveyor.com/api/projects/status/q408tg5jd9bm0jak/branch/master?svg=true)](https://ci.appveyor.com/project/geoperez/swan-aspnetcore/branch/master)
[![Coverage Status](https://coveralls.io/repos/github/unosquare/swan-aspnetcore/badge.svg?branch=master)](https://coveralls.io/github/unosquare/swan-aspnetcore?branch=master)
# <img src="https://github.com/unosquare/swan/raw/master/swan-logo-32.png"></img> SWAN: Stuff We All Need

*:star: Please star this project if you find it useful!*

Expand All @@ -21,81 +22,95 @@ PM> Install-Package Unosquare.Swan.AspNetCore

Here we can find very useful code to use in our project configuration. All of this you can use it in your Startup.cs file, see our [Sample Project](https://github.com/unosquare/swan-aspnetcore/tree/master/src/Unosquare.Swan.AspNetCore.Sample) for more reference.

### Case 1: Add BearerTokenAuthentication and Use BearerTokenAuthentication
### Add BearerTokenAuthentication and Use BearerTokenAuthentication

Add BearerTokenAuthentication this only adds services to your project.
Add BearerTokenAuthentication adds the services that are going to be used in your application. This method uses the Jwt schemes and adds a JwtBearer with the Token Validation Parameters that you configure. Jwt stands for [JSON Web Tokens](https://jwt.io/introduction/)

Use BearerTokenAuthentication is important because it gives the application the requirements to use authentication and authorization with JWT (JSON Web tokens).
Use BearerTokenAuthentication is important because it gives the application the requirements to use authentication and authorization with JWT.

With this configuration, you just need to add the data annotation [Authorize] to your API to say that the user needs to be authorized to access that part of your project.

This two are used together because you need to add the bearer token authentication to the services to use the bearer token authentication. You just need to added in your `ConfigureServices` and your `Configure`.
This two are used together because you need to add the bearer token authentication to the services to use the bearer token authentication in your application. You just need to added in your `ConfigureServices` and your `Configure`.

```csharp
// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
// Extension method to add Bearer authentication
services.AddBearerTokenAuthentication(ValidationParameters);
// Extension method to add Bearer authentication
services.AddBearerTokenAuthentication(ValidationParameters);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
public void Configure(IApplicationBuilder app)
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Use the bearer token provider and check Admin and Pass.word as valid credentials
app.UseBearerTokenAuthentication(
ValidationParameters,
(username, password, grantType, clientId) =>
{
if (username != "Admin" || password != "Pass.word")
return Task.FromResult<ClaimsIdentity>(null);

var claim = new ClaimsIdentity("Bearer");
claim.AddClaim(new Claim(ClaimTypes.Name, username));

return Task.FromResult(claim);
}, (identity, obj) =>
{
// This action is optional
obj["test"] = "OK";
return Task.FromResult(obj);
});
// Use the bearer token provider and check Admin and Pass.word as valid credentials
app.UseBearerTokenAuthentication(
ValidationParameters,
(username, password, grantType, clientId) =>
{
if (username != "Admin" || password != "Pass.word")
return Task.FromResult<ClaimsIdentity>(null);

var claim = new ClaimsIdentity("Bearer");
claim.AddClaim(new Claim(ClaimTypes.Name, username));

return Task.FromResult(claim);
}, (identity, obj) =>
{
// This action is optional
obj["test"] = "OK";
return Task.FromResult(obj);
});
}
```

### Case 2: EntityFrameworkLogger

Represents a Logger using EntityFramework
### EntityFrameworkLogger

#### Example 1: AddEntityFramework

Adds an entity framework logger. This logger is used in the `Configure` method of your Startup.cs file.
Represents a Logger using EntityFramework. Adds an entity framework logger. It will help you log to your Database everything necessary to know what’s going on in your application. This logger is used in the `Configure` method of your Startup.cs file.

```csharp
// you need to declare as parameter of type ILoggerFactory
loggerFactory.AddEntityFramework<SampleDbContext, Models.LogEntry>(app.ApplicationServices);
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Log levels set in your configuration
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
// Here we add the entity framework with the database and the model to utilize
loggerFactory.AddEntityFramework<SampleDbContext, Models.LogEntry>(app.ApplicationServices);
}
```

### Case 3: AuditTrail
### AuditTrail

This is an extension method to add AuditTrail to a DbContext.
[Audit Trails](https://github.com/unosquare/ef-enterpriseextensions) is a task to save the changes to any operation perform in a record. In other words, capture what change between any data saving. This operation is important in many system and you can accomplish with these extensions easily. The AuditTrailController can be attached to your BusinessDbContext and setup which Entities will be recorded in the three CRUD actions supported, create, update and delete.

```csharp
// TODO: Example
public class SampleDbContext : BusinessDbContext
{
public SampleDbContext(DbContextOptions<SampleDbContext> options, IHttpContextAccessor httpContextAccessor)
: base(options)
{
var auditController = new AuditTrailController<SampleDbContext, AuditTrailEntry>(this,
httpContextAccessor?.HttpContext?.User?.Identity?.Name);
auditController.AddTypes(ActionFlags.Create, new[] {typeof(Product)});

AddController(auditController);
}

public DbSet<AuditTrailEntry> AuditTrailEntries { get; set; }
}
```

### Case 4: UseJsonExceptionHandler
### UseJsonExceptionHandler

It's very useful to see exceptions in JSON format.
It's very useful to see exceptions in JSON format. You can use this extension to add a very good way to debug your application, you just need to add this to your application builder in the Configure method of your Startup.cs

```csharp
// Response an exception as JSON at error
app.UseJsonExceptionHandler();
```

### Case 5: UseFallback
### UseFallback

Uses the fallback to redirect everything without extension.
Uses the fallback to redirect everything without extension. When the application encountered something without an extension this will help to redirect to the index page or where ever you define to.

```csharp
// Redirect anything without extension to index.html
Expand Down
14 changes: 0 additions & 14 deletions src/Unosquare.Swan.AspNetCore/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@ public static class Extensions
/// </summary>
public const string JsonMimeType = "application/json";

// TODO: Do we need this?
/// <summary>
/// Setups the cookies.
/// </summary>
/// <param name="identityOptions">The identity options.</param>
//public static void SetupCookies(this IdentityOptions identityOptions)
//{
// identityOptions.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents
// {
// // Skip the login
// OnRedirectToLogin = context => Task.FromResult(0)
// };
//}

/// <summary>
/// Uses the json exception handler.
/// </summary>
Expand Down

0 comments on commit 852febd

Please sign in to comment.