Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9ddaeaf

Browse files
Merge pull request #25 from livealvi/auth
add: Authorization
2 parents 8318acb + aa3107e commit 9ddaeaf

File tree

7 files changed

+68
-16
lines changed

7 files changed

+68
-16
lines changed

‎.idea/.idea.Web-Series-API--ASP.NET/.idea/workspace.xml‎

Lines changed: 19 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎BusinessLogicLayer/Services/AuthService.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,11 @@ public static LoginModel EmailCheck(string email)
4242
var isFind = mapper.Map<Login, LoginModel>(DataAccessFactory.AuthDataAccess().GetByEmail(email));
4343
return isFind;
4444
}
45+
46+
public static bool isAuthenticated(string token)
47+
{
48+
var result = DataAccessFactory.AuthDataAccess().isAuthenticated(token);
49+
return result;
50+
}
4551
}
4652
}

‎DataAccessLayer/Interfaces/IAuth.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface IAuth<T, ID>
1111
{
1212
Token Authenticate(Login login);
1313
T GetByEmail(ID email);
14-
bool isAuthenticated(T obj);
14+
bool isAuthenticated(ID obj);
1515
bool Logout(T obj);
1616
}
1717
}

‎DataAccessLayer/Repo/AuthRepo.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ public Login GetByEmail(string email)
3939
return db.Logins.FirstOrDefault(e => e.Email.Equals((email)));
4040
}
4141

42-
public bool isAuthenticated(Loginobj)
42+
public bool isAuthenticated(stringtoken)
4343
{
44-
throw new NotImplementedException();
44+
if (token == null) return false;
45+
var result = db.Tokens.Any(tok => tok.TokenData.Equals((token)) && tok.ExpiredAt == null);
46+
return result;
4547
}
4648

4749
public bool Logout(Login obj)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using BusinessLogicLayer.Services;
2+
using System.Web.Http.Filters;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Web.Http;
8+
using System.Web.Http.Controllers;
9+
10+
namespace Web_Series_API__ASP.NET.Auth
11+
{
12+
public class TokenChecker :AuthorizationFilterAttribute
13+
{
14+
public override void OnAuthorization(HttpActionContext actionContext)
15+
{
16+
var header = actionContext.Request.Headers.Authorization;
17+
if (header == null)
18+
{
19+
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized,"Token not found");
20+
}
21+
else
22+
{
23+
if (AuthService.isAuthenticated((header.ToString())))
24+
{
25+
26+
}
27+
else
28+
{
29+
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized,"Token is not valid or expired");
30+
}
31+
}
32+
base.OnAuthorization(actionContext);
33+
}
34+
}
35+
}

‎Web-Series-API--ASP.NET/Controllers/UserController.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
using System.Net.Http;
88
using System.Web.Http;
99
using System.Web.Http.Cors;
10+
using Web_Series_API__ASP.NET.Auth;
1011

1112
namespace Web_Series_API__ASP.NET.Controllers
1213
{
14+
[TokenChecker]
1315
public class UserController : ApiController
1416
{
1517
[EnableCors("*", "*", "*")]

‎Web-Series-API--ASP.NET/Web-Series-API--ASP.NET.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
</ItemGroup>
9797
<ItemGroup>
9898
<Compile Include="App_Start\WebApiConfig.cs" />
99+
<Compile Include="Auth\TokenChecker.cs" />
99100
<Compile Include="Controllers\AuthController.cs" />
100101
<Compile Include="Controllers\CategoryController.cs" />
101102
<Compile Include="Controllers\ExpenseController.cs" />

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /