Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
JWTVueDemo
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jay
JWTVueDemo
Commits
980813ab
Commit
980813ab
authored
Jun 18, 2020
by
Jay
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
web api Role 權限
parent
75b2d744
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
100 additions
and
19 deletions
+100
-19
JWTVueDemo/ClientApp/public/index.html
+0
-1
JWTVueDemo/ClientApp/src/App.vue
+11
-7
JWTVueDemo/ClientApp/src/api/auth.js
+6
-0
JWTVueDemo/ClientApp/src/api/instance.js
+6
-6
JWTVueDemo/ClientApp/src/store/authData.js
+0
-1
JWTVueDemo/ClientApp/src/views/Home.vue
+19
-2
JWTVueDemo/Const/Roles.cs
+10
-0
JWTVueDemo/Controllers/AuthController.cs
+48
-2
No files found.
JWTVueDemo/ClientApp/public/index.html
View file @
980813ab
...
@@ -9,7 +9,6 @@
...
@@ -9,7 +9,6 @@
<title></title>
<title></title>
<link
rel=
"stylesheet"
href=
"https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900"
>
<link
rel=
"stylesheet"
href=
"https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900"
>
<link
rel=
"stylesheet"
href=
"https://cdn.jsdelivr.net/npm/@mdi/font@latest/css/materialdesignicons.min.css"
>
<link
rel=
"stylesheet"
href=
"https://cdn.jsdelivr.net/npm/@mdi/font@latest/css/materialdesignicons.min.css"
>
<script
src=
"./ckeditor/ckeditor.js"
></script>
</head>
</head>
<body>
<body>
...
...
JWTVueDemo/ClientApp/src/App.vue
View file @
980813ab
...
@@ -28,7 +28,12 @@
...
@@ -28,7 +28,12 @@
<v-footer
color=
"indigo"
app
>
<v-footer
color=
"indigo"
app
>
<span
class=
"white--text"
>
©
2020
</span>
<span
class=
"white--text"
>
©
2020
</span>
</v-footer>
</v-footer>
<v-snackbar
v-model=
"snackbar.visible"
:timeout=
"0"
:top=
"true"
:color=
"snackbar.color"
>
{{
snackbar
.
message
}}
</v-snackbar>
<v-snackbar
v-model=
"snackbar.visible"
:timeout=
"0"
:top=
"true"
:color=
"snackbar.color"
>
{{
snackbar
.
message
}}
</v-snackbar>
</v-app>
</v-app>
<v-app
v-else
>
<v-app
v-else
>
<router-view
/>
<router-view
/>
...
@@ -43,13 +48,12 @@ export default {
...
@@ -43,13 +48,12 @@ export default {
data
:
()
=>
({
data
:
()
=>
({
sideBarOpen
:
false
,
sideBarOpen
:
false
,
changePasswordDialogVisible
:
false
,
changePasswordDialogVisible
:
false
,
requiredRule
:
[
(
v
)
=>
!!
v
||
'Required'
],
requiredRule
:
[
v
=>
!!
v
||
'Required'
]
}),
}),
created
()
{
created
()
{
const
localJWT
=
localStorage
.
getItem
(
'authJWT'
);
const
localJWT
=
localStorage
.
getItem
(
'authJWT'
);
if
(
localJWT
)
{
if
(
localJWT
)
{
this
.
$store
.
commit
(
'SetAuthJWT'
,
{
authJWT
:
localJWT
});
this
.
$store
.
commit
(
'SetAuthJWT'
,
{
authJWT
:
localJWT
});
this
.
$store
.
dispatch
(
'basicData'
);
}
else
{
}
else
{
this
.
$router
.
push
(
'/login'
);
this
.
$router
.
push
(
'/login'
);
}
}
...
@@ -58,7 +62,7 @@ export default {
...
@@ -58,7 +62,7 @@ export default {
methods
:
{
methods
:
{
Logout
()
{
Logout
()
{
this
.
$store
.
dispatch
(
'Logout'
);
this
.
$store
.
dispatch
(
'Logout'
);
}
,
}
},
},
computed
:
{
computed
:
{
pageTitle
()
{
pageTitle
()
{
...
@@ -69,7 +73,7 @@ export default {
...
@@ -69,7 +73,7 @@ export default {
},
},
getLinks
()
{
getLinks
()
{
return
this
.
$router
.
options
.
routes
.
filter
(
return
this
.
$router
.
options
.
routes
.
filter
(
(
link
)
=>
link
.
isMenu
//&& (link.admin === undefined || link.admin === this.$store.state.authData.isAdmin)
link
=>
link
.
isMenu
//&& (link.admin === undefined || link.admin === this.$store.state.authData.isAdmin)
);
);
},
},
isAdmin
()
{
isAdmin
()
{
...
@@ -77,9 +81,9 @@ export default {
...
@@ -77,9 +81,9 @@ export default {
},
},
snackbar
()
{
snackbar
()
{
return
this
.
$store
.
state
.
snackbar
;
return
this
.
$store
.
state
.
snackbar
;
}
,
}
},
},
watch
:
{}
,
watch
:
{}
};
};
</
script
>
</
script
>
<
style
lang=
"scss"
>
<
style
lang=
"scss"
>
...
...
JWTVueDemo/ClientApp/src/api/auth.js
View file @
980813ab
import
{
instance
}
from
'./instance'
;
import
{
instance
}
from
'./instance'
;
export
const
login
=
({
Account
,
Password
})
=>
instance
.
post
(
'/Auth/Login'
,
{
Account
,
Password
});
export
const
login
=
({
Account
,
Password
})
=>
instance
.
post
(
'/Auth/Login'
,
{
Account
,
Password
});
export
default
{
testAdmin
:
()
=>
instance
.
get
(
'/Auth/TestAdmin'
),
testUser
:
()
=>
instance
.
get
(
'/Auth/TestUser'
),
testAdminAndUser
:
()
=>
instance
.
get
(
'/Auth/TestAdminAndUser'
),
};
JWTVueDemo/ClientApp/src/api/instance.js
View file @
980813ab
...
@@ -3,10 +3,10 @@ import store from '../store/index';
...
@@ -3,10 +3,10 @@ import store from '../store/index';
import
router
from
'@/router'
;
import
router
from
'@/router'
;
import
{
authDataAction
}
from
'../store/authData'
;
import
{
authDataAction
}
from
'../store/authData'
;
const
instance
=
axios
.
create
({
const
instance
=
axios
.
create
({
baseURL
:
process
.
env
.
VUE_APP_API_BASE_URL
baseURL
:
process
.
env
.
VUE_APP_API_BASE_URL
,
});
});
instance
.
interceptors
.
request
.
use
(
config
=>
{
instance
.
interceptors
.
request
.
use
(
(
config
)
=>
{
const
authJWT
=
store
.
state
.
authData
.
authJWT
;
const
authJWT
=
store
.
state
.
authData
.
authJWT
;
if
(
authJWT
)
{
if
(
authJWT
)
{
config
.
headers
.
Authorization
=
'Bearer '
+
authJWT
;
config
.
headers
.
Authorization
=
'Bearer '
+
authJWT
;
...
@@ -14,14 +14,14 @@ instance.interceptors.request.use(config => {
...
@@ -14,14 +14,14 @@ instance.interceptors.request.use(config => {
return
config
;
return
config
;
});
});
instance
.
interceptors
.
response
.
use
(
instance
.
interceptors
.
response
.
use
(
config
=>
{
(
config
)
=>
{
// console.log(config);
// console.log(config);
return
config
;
//.data;
return
config
;
//.data;
},
},
error
=>
{
(
error
)
=>
{
if
(
error
.
response
&&
(
error
.
response
.
status
===
401
||
error
.
response
.
status
===
403
))
{
if
(
error
.
response
&&
(
error
.
response
.
status
===
401
||
error
.
response
.
status
===
403
))
{
store
.
dispatch
(
authDataAction
.
Logout
);
//
store.dispatch(authDataAction.Logout);
router
.
push
(
'/'
);
//
router.push('/');
}
}
return
Promise
.
reject
(
error
);
return
Promise
.
reject
(
error
);
}
}
...
...
JWTVueDemo/ClientApp/src/store/authData.js
View file @
980813ab
...
@@ -50,7 +50,6 @@ const authData = {
...
@@ -50,7 +50,6 @@ const authData = {
if
(
data
.
Success
)
{
if
(
data
.
Success
)
{
commit
(
SetAuthJWT
,
{
authJWT
:
data
.
Data
});
commit
(
SetAuthJWT
,
{
authJWT
:
data
.
Data
});
localStorage
.
setItem
(
'authJWT'
,
data
.
Data
);
localStorage
.
setItem
(
'authJWT'
,
data
.
Data
);
dispatch
(
'basicData'
);
}
}
return
data
;
return
data
;
},
},
...
...
JWTVueDemo/ClientApp/src/views/Home.vue
View file @
980813ab
<
template
>
<
template
>
<div
class=
"home"
>
<div
class=
"home"
>
<!--
<v-btn>
Home
</v-btn>
-->
<v-btn
@
click=
"testAdmin"
>
TestAdmin
</v-btn>
<v-btn
@
click=
"testUser"
>
TestUser
</v-btn>
<v-btn
@
click=
"testAdminAndUser"
>
TestAdminAndUser
</v-btn>
</div>
</div>
</
template
>
</
template
>
<
script
>
<
script
>
// @ is an alias to /src
// @ is an alias to /src
import
{
setPageTitleMixin
}
from
'../mixins/setPageTitleMixin'
;
import
{
setPageTitleMixin
}
from
'../mixins/setPageTitleMixin'
;
import
authApi
from
'../api/auth'
;
export
default
{
export
default
{
mixins
:
[
setPageTitleMixin
],
mixins
:
[
setPageTitleMixin
],
components
:
{},
components
:
{},
name
:
'home'
name
:
'home'
,
methods
:
{
async
testAdmin
()
{
const
response
=
await
authApi
.
testAdmin
();
console
.
log
(
response
);
},
async
testUser
()
{
const
response
=
await
authApi
.
testUser
();
console
.
log
(
response
);
},
async
testAdminAndUser
()
{
const
response
=
await
authApi
.
testAdminAndUser
();
console
.
log
(
response
);
}
}
};
};
</
script
>
</
script
>
JWTVueDemo/Const/Roles.cs
0 → 100644
View file @
980813ab
namespace
JWTVueDemo.Const
{
public
static
class
Roles
{
public
const
string
Role
=
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
;
public
const
string
Admin
=
"Admin"
;
public
const
string
User
=
"User"
;
public
const
string
AdminAndUser
=
"Admin,User"
;
}
}
JWTVueDemo/Controllers/AuthController.cs
View file @
980813ab
using
JWTVueDemo.Models
;
using
JWTVueDemo.Const
;
using
JWTVueDemo.Models
;
using
JWTVueDemo.Models.RequestModel
;
using
JWTVueDemo.Models.RequestModel
;
using
Microsoft.AspNetCore.Authorization
;
using
Microsoft.AspNetCore.Mvc
;
using
Microsoft.AspNetCore.Mvc
;
using
Microsoft.Extensions.Options
;
using
Microsoft.Extensions.Options
;
using
Microsoft.IdentityModel.Tokens
;
using
Microsoft.IdentityModel.Tokens
;
...
@@ -31,13 +33,14 @@ namespace JWTVueDemo.Controllers
...
@@ -31,13 +33,14 @@ namespace JWTVueDemo.Controllers
};
};
if
(
loginInfo
.
Account
.
Equals
(
"admin"
)
&&
loginInfo
.
Password
.
Equals
(
"123"
))
if
(
loginInfo
.
Account
.
Equals
(
"admin"
)
&&
loginInfo
.
Password
.
Equals
(
"123"
))
{
{
var
isAdmin
=
true
;
var
key
=
new
SymmetricSecurityKey
(
Encoding
.
UTF8
.
GetBytes
(
AppSettings
.
JWTSecret
));
var
key
=
new
SymmetricSecurityKey
(
Encoding
.
UTF8
.
GetBytes
(
AppSettings
.
JWTSecret
));
var
tokenDescriptor
=
new
SecurityTokenDescriptor
var
tokenDescriptor
=
new
SecurityTokenDescriptor
{
{
Subject
=
new
ClaimsIdentity
(
new
Claim
[]
Subject
=
new
ClaimsIdentity
(
new
Claim
[]
{
{
new
Claim
(
"UserId"
,
"admin"
),
new
Claim
(
"UserId"
,
"admin"
),
//
new Claim("roles",isAdmin?Roles.Admin:Roles.User)
new
Claim
(
"roles"
,
isAdmin
?
Roles
.
Admin
:
Roles
.
User
)
}),
}),
Expires
=
DateTime
.
UtcNow
.
AddDays
(
30
),
Expires
=
DateTime
.
UtcNow
.
AddDays
(
30
),
SigningCredentials
=
new
SigningCredentials
(
key
,
SecurityAlgorithms
.
HmacSha256Signature
)
SigningCredentials
=
new
SigningCredentials
(
key
,
SecurityAlgorithms
.
HmacSha256Signature
)
...
@@ -55,5 +58,48 @@ namespace JWTVueDemo.Controllers
...
@@ -55,5 +58,48 @@ namespace JWTVueDemo.Controllers
return
response
;
return
response
;
}
}
}
}
[
HttpGet
(
"[action]"
)]
public
BaseResponse
<
DateTime
>
Test
()
{
return
new
BaseResponse
<
DateTime
>
{
Data
=
DateTime
.
Now
,
Msg
=
"Test"
,
Success
=
true
};
}
[
Authorize
(
Roles
=
Roles
.
Admin
)]
[
HttpGet
(
"[action]"
)]
public
BaseResponse
<
DateTime
>
TestAdmin
()
{
return
new
BaseResponse
<
DateTime
>
{
Data
=
DateTime
.
Now
,
Msg
=
"TestAdmin"
,
Success
=
true
};
}
[
Authorize
(
Roles
=
Roles
.
User
)]
[
HttpGet
(
"[action]"
)]
public
BaseResponse
<
DateTime
>
TestUser
()
{
return
new
BaseResponse
<
DateTime
>
{
Data
=
DateTime
.
Now
,
Msg
=
"TestUser"
,
Success
=
true
};
}
[
Authorize
(
Roles
=
Roles
.
AdminAndUser
)]
[
HttpGet
(
"[action]"
)]
public
BaseResponse
<
DateTime
>
TestAdminAndUser
()
{
return
new
BaseResponse
<
DateTime
>
{
Data
=
DateTime
.
Now
,
Msg
=
"TestAdminAndUser"
,
Success
=
true
};
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment