asp.net core配合vue實(shí)現(xiàn)后端驗(yàn)證碼邏輯
概述
網(wǎng)上的前端驗(yàn)證碼邏輯總感覺(jué)不安全,驗(yàn)證碼建議還是使用后端配合驗(yàn)證。
如果產(chǎn)品確定可以上網(wǎng)的話,就可以使用騰訊,百度等第三方驗(yàn)證,對(duì)接方便。但是產(chǎn)品可能內(nèi)網(wǎng)部署,就必須自己寫了。
本文章就是基于這一點(diǎn)來(lái)實(shí)現(xiàn)的。
前端驗(yàn)證碼顯示一個(gè)圖片,后端生成圖片。
部分原理
1.前端調(diào)用生端獲取圖片時(shí),傳入一個(gè)roomID,后端生成一個(gè)4位驗(yàn)征碼,放入redis中。然后生成一個(gè)圖片返回。
2.前端顯示圖片,登錄時(shí)將roomID和填寫的驗(yàn)證碼,一并提交,登錄接口根據(jù)roomId從redis中取出驗(yàn)證碼判斷是否正確。
這樣就相當(dāng)于后端驗(yàn)證了。
大家覺(jué)得有問(wèn)題什么,可以進(jìn)行評(píng)論。謝謝。
源碼
前端部分代碼
<template>
<div class="login-container">
<vue-particles
color="#ffffff"
:particleOpacity="0.7"
:particlesNumber="50"
shapeType="circle"
:particleSize="4"
linesColor="#dedede"
:linesWidth="1"
:lineLinked="true"
:lineOpacity="0.4"
:linesDistance="150"
:moveSpeed="2"
:hoverEffect="true"
hoverMode="grab"
:clickEffect="true"
clickMode="push"
></vue-particles>
<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
<div class="title-container">
<h3 class="title">智能綜合管理系統(tǒng)</h3>
</div>
<el-form-item prop="username">
<span class="svg-container">
<svg-icon icon-class="user" />
</span>
<el-input ref="username" v-model="loginForm.username" placeholder="用戶名" name="username" type="text" tabindex="1" autocomplete="on" />
</el-form-item>
<el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
<el-form-item prop="password">
<span class="svg-container">
<svg-icon icon-class="password" />
</span>
<el-input
:key="passwordType"
ref="password"
v-model="loginForm.password"
:type="passwordType"
placeholder="密碼"
name="password"
tabindex="2"
autocomplete="on"
@keyup.native="checkCapslock"
@blur="capsTooltip = false"
@keyup.enter.native="handleLogin"
/>
<span class="show-pwd" @click="showPwd">
<svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
</span>
</el-form-item>
</el-tooltip>
<el-form-item prop="yzm">
<span class="svg-container">
<svg-icon icon-class="password" />
</span>
<el-input type="text" v-model="loginForm.verifyCode" maxlength="4" placeholder="驗(yàn)證碼" />
<div class="identifyCode" @click="refreshCode">
<el-image :src="verifyImageUrl"></el-image>
</div>
</el-form-item>
<el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px" @click.native.prevent="handleLogin">登錄</el-button>
</el-form>
</div>
</template>
<script>
import { validUsername } from '@/utils/validate'
import Identify from './components/Identify'
import { uuid } from 'vue-uuid'; // uuid object is also exported to things
// outside Vue instance.
export default {
name: 'Login',
components: { Identify },
data() {
const validateUsername = (rule, value, callback) => {
if (!validUsername(value)) {
callback(new Error('請(qǐng)輸入正確的用戶名'))
} else {
callback()
}
}
const validatePassword = (rule, value, callback) => {
if (value.length < 6) {
callback(new Error('密碼最少6位'))
} else {
callback()
}
}
return {
loginForm: {
username: 'admin',
password: '111111',
roomId: '',
verifyCode: ''
},
loginRules: {
username: [{ required: true, trigger: 'blur', validator: validateUsername }],
password: [{ required: true, trigger: 'blur', validator: validatePassword }]
},
passwordType: 'password',
capsTooltip: false,
loading: false,
showDialog: false,
redirect: undefined,
otherQuery: {},
identifyCodes: '1234567890',
identifyCode: '',
// verifyImageRoomId: '',
verifyImageUrl: '',
}
},
watch: {
$route: {
handler: function (route) {
const query = route.query
if (query) {
this.redirect = query.redirect
this.otherQuery = this.getOtherQuery(query)
}
},
immediate: true
}
},
created() {
// window.addEventListener('storage', this.afterQRScan)
// this.identifyCode = ''
// this.makeCode(this.identifyCodes, 4)
this.refreshCode()
},
mounted() {
if (this.loginForm.username === '') {
this.$refs.username.focus()
} else if (this.loginForm.password === '') {
this.$refs.password.focus()
}
},
destroyed() {
// window.removeEventListener('storage', this.afterQRScan)
},
methods: {
checkCapslock(e) {
const { key } = e
this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
},
showPwd() {
if (this.passwordType === 'password') {
this.passwordType = ''
} else {
this.passwordType = 'password'
}
this.$nextTick(() => {
this.$refs.password.focus()
})
},
createUuid() {
let uuidV4 = uuid.v4().replace('-', '').replace('-', '').replace('-', '').replace('-', '')
this.verifyImageRoomId = uuidV4
this.verifyImageUrl = '/api/Operator/getCaptchaImage/120/40/' + this.verifyImageRoomId
console.log(uuidV4)
},
handleLogin() {
this.$refs.loginForm.validate(valid => {
if (valid) {
this.loading = true
this.$store.dispatch('user/login', this.loginForm)
.then(() => {
this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
this.loading = false
})
.catch(() => {
this.loading = false
})
} else {
console.log('error submit!!')
return false
}
})
},
getOtherQuery(query) {
return Object.keys(query).reduce((acc, cur) => {
if (cur !== 'redirect') {
acc[cur] = query[cur]
}
return acc
}, {})
},
// 生成隨機(jī)數(shù)
randomNum(min, max) {
return Math.floor(Math.random() * (max - min) + min)
},
// 切換驗(yàn)證碼
refreshCode() {
let uuidV4 = uuid.v4().replace('-', '').replace('-', '').replace('-', '').replace('-', '')
this.loginForm.roomId = uuidV4
this.verifyImageUrl = '/api/Operator/getCaptchaImage/120/40/' + this.loginForm.roomId
console.log(uuidV4)
},
// 生成四位隨機(jī)驗(yàn)證碼
makeCode(o, l) {
for (let i = 0; i < l; i++) {
this.identifyCode += this.identifyCodes[
this.randomNum(0, this.identifyCodes.length)
]
}
console.log(this.identifyCode)
}
}
}
</script>
<style lang="scss">
/* 修復(fù)input 背景不協(xié)調(diào) 和光標(biāo)變色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
.login-container .el-input input {
color: $cursor;
}
}
/* reset element-ui css */
.login-container {
background: url("~@/assets/background.jpg") no-repeat;
min-height: 100vh;
.el-input {
display: inline-block;
height: 47px;
width: 85%;
input {
background: transparent;
border: 0px;
-webkit-appearance: none;
border-radius: 0px;
padding: 12px 5px 12px 15px;
color: $light_gray;
height: 47px;
caret-color: $cursor;
&:-webkit-autofill {
box-shadow: 0 0 0px 1000px $bg inset !important;
-webkit-text-fill-color: $cursor !important;
}
}
}
.el-form-item {
border: 1px solid rgba(255, 255, 255, 0.1);
background: rgba(0, 0, 0, 0.1);
border-radius: 5px;
color: #454545;
.el-form-item__error {
color: rgb(223, 223, 176);
}
}
}
</style>
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
min-height: 100%;
width: 100%;
background-color: $bg;
overflow: hidden;
.login-form {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
width: 520px;
max-width: 100%;
padding: 160px 35px 0;
margin: 0 auto;
overflow: hidden;
}
.tips {
font-size: 14px;
color: #fff;
margin-bottom: 10px;
span {
&:first-of-type {
margin-right: 16px;
}
}
}
.svg-container {
padding: 6px 5px 6px 15px;
color: $dark_gray;
vertical-align: middle;
width: 30px;
display: inline-block;
}
.title-container {
position: relative;
.title {
font-size: 42px;
color: $light_gray;
margin: 0px auto 40px auto;
text-align: center;
font-weight: bold;
}
}
.show-pwd {
position: absolute;
right: 10px;
top: 7px;
font-size: 16px;
color: $dark_gray;
cursor: pointer;
user-select: none;
}
.identifyCode {
position: absolute;
right: 10px;
top: 5px;
}
.thirdparty-button {
position: absolute;
right: 0;
bottom: 6px;
}
@media only screen and (max-width: 470px) {
.thirdparty-button {
display: none;
}
}
}
</style>
后端接口
/// <summary>
/// 獲取驗(yàn)證碼
/// </summary>
/// <returns></returns>
[HttpGet("getCaptchaImage/{width:int}/{height:int}/{roomId}")]
public IActionResult GetCaptchaImage(int width, int height, string roomId)
{
Console.WriteLine(roomId);
//int width = 100;
//int height = 36;
var captchaCode = Captcha.GenerateCaptchaCode();
var result = Captcha.GenerateCaptchaImage(width, height, captchaCode);
string redisKey = "VerifyCode_" + roomId;
Startup.redisDb.StringSet(redisKey, captchaCode, new TimeSpan(0, 5, 0));
Stream s = new MemoryStream(result.CaptchaByteData);
return new FileStreamResult(s, "image/png");
}
/// <summary>
/// 登錄
/// </summary>
/// <returns></returns>
[HttpPost("login")]
public ApiResponseData Login(LoginDto loginInfo)
{
if (string.IsNullOrWhiteSpace(loginInfo.username))
return ApiResponse.Error("用戶名不能為空");
if (string.IsNullOrWhiteSpace(loginInfo.password))
return ApiResponse.Error("密碼不能為空");
Entity.BaseOperator operInfo = Db
.Select<BaseOperator>()
.Where(a => a.OperatorCode == loginInfo.username && a.Password == loginInfo.password.ToLower() && a.Status == 1 && a.IsDel == false).ToOne();
if (operInfo == null)
return ApiResponse.Error("用戶名或者密碼不正確");
bool verifyResult = Captcha.ValidateCaptchaCode(loginInfo.RoomId, loginInfo.VerifyCode);
if(verifyResult == false)
return ApiResponse.Error("驗(yàn)證碼不正確");
//登錄時(shí)重新生成token,一個(gè)用戶只能在一個(gè)地方登錄
string token = System.Guid.NewGuid().ToString().Replace("-", "");
Db.Update<BaseOperator>(operInfo.OperatorId)
.Set(a => a.Token, token)
.ExecuteAffrows();
dynamic outJson = new ExpandoObject();//初始化一個(gè)不包含任何成員的ExpandoObject
outJson.token = token;
//存入redis
string redisKey = "UserInfo_" + token;
Startup.redisDb.StringSet(redisKey, operInfo.OperatorId, new TimeSpan(24, 0, 0));
return ApiResponse.Succ(outJson);
}
到此這篇關(guān)于asp.net core配合vue實(shí)現(xiàn)后端驗(yàn)證碼邏輯的文章就介紹到這了,更多相關(guān)asp.net core驗(yàn)證碼 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- ASP.NET?Core?模型驗(yàn)證消息的本地化新姿勢(shì)詳解
- ASP.NET?Core?6.0?基于模型驗(yàn)證的數(shù)據(jù)驗(yàn)證功能
- ASP.NET?Core中Cookie驗(yàn)證身份用法詳解
- asp.net core3.1cookie和jwt混合認(rèn)證授權(quán)實(shí)現(xiàn)多種身份驗(yàn)證方案
- [Asp.Net Core]用Blazor Server Side實(shí)現(xiàn)圖片驗(yàn)證碼
- ASP.NET Core實(shí)現(xiàn)自定義WebApi模型驗(yàn)證詳解
- asp.net core系列之模型綁定和驗(yàn)證方法
- ASP.NET Core WebApi中使用FluentValidation驗(yàn)證數(shù)據(jù)模型的方法
相關(guān)文章
asp.net 動(dòng)態(tài)創(chuàng)建TextBox控件及狀態(tài)數(shù)據(jù)如何加載
接著上文Asp.net TextBox的TextChanged事件你真的清楚嗎?這里我們來(lái)說(shuō)說(shuō)狀態(tài)數(shù)據(jù)時(shí)如何加載的,需要的朋友可以參考下2012-12-12
Asp.net MVC scheduler的實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Asp.net MVC scheduler的實(shí)現(xiàn)方法詳解的相關(guān)資料,希望通過(guò)本文大家能夠?qū)崿F(xiàn)這樣的方法,需要的朋友可以參考下2017-10-10
ASP.net基礎(chǔ)知識(shí)之常見(jiàn)錯(cuò)誤分析
ASP.net基礎(chǔ)知識(shí)之常見(jiàn)錯(cuò)誤分析...2007-07-07
Asp.Net MVC4通過(guò)id更新表單內(nèi)容的思路詳解
一個(gè)表單一旦創(chuàng)建完,其中大部分的字段便不可再編輯。只能編輯其中部分字段。下面通過(guò)本文給大家分享Asp.Net MVC4通過(guò)id更新表單內(nèi)容的思路詳解,需要的朋友參考下吧2017-07-07
IIS和.NET(1.1/2.0)的安裝順序及錯(cuò)誤解決方法
安裝順序及錯(cuò)誤的解決方法:基于.net2.0的情況與基于.net1.1的情況,分別給予解決方法,遇到此問(wèn)題的朋友可以了解下,或許對(duì)你的學(xué)習(xí)有所幫助2013-02-02
防SQL注入 生成參數(shù)化的通用分頁(yè)查詢語(yǔ)句
前些時(shí)間看了玉開(kāi)兄的“如此高效通用的分頁(yè)存儲(chǔ)過(guò)程是帶有sql注入漏洞的”這篇文章,才突然想起某個(gè)項(xiàng)目也是使用了累似的通用分頁(yè)存儲(chǔ)過(guò)程。2010-07-07
.NET異步編程總結(jié)----四種實(shí)現(xiàn)模式代碼總結(jié)
本篇文章主要介紹了.NET異步編程總結(jié)----四種實(shí)現(xiàn)模式,詳細(xì)的介紹了每種方法的實(shí)現(xiàn)和實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12
asp.net 讀取文本文件并插入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
最近我司和招行有合作,招行給財(cái)務(wù)的是一個(gè)txt格式的賬務(wù)文本文件,文本文件包含很多內(nèi)容,對(duì)賬只需要用到其中一部分內(nèi)容。2010-04-04

