add admin session and some resources
implement login function
This commit is contained in:
parent
b19a32c569
commit
8fb438238d
@ -46,6 +46,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Model\Admin\AdminModel.cs" />
|
<Compile Include="Model\Admin\AdminModel.cs" />
|
||||||
|
<Compile Include="Model\Admin\AdminSession.cs" />
|
||||||
<Compile Include="Model\Marker.cs" />
|
<Compile Include="Model\Marker.cs" />
|
||||||
<Compile Include="Model\AlarmItem.cs" />
|
<Compile Include="Model\AlarmItem.cs" />
|
||||||
<Compile Include="Server.cs" />
|
<Compile Include="Server.cs" />
|
||||||
|
17
Lora-Map/Model/Admin/AdminSession.cs
Normal file
17
Lora-Map/Model/Admin/AdminSession.cs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Fraunhofer.Fit.IoT.LoraMap.Model.Admin {
|
||||||
|
class AdminSession {
|
||||||
|
public Boolean IsLoggedin { get; internal set; }
|
||||||
|
public static Int64 GetRandomSessionid() {
|
||||||
|
Byte[] buf = new Byte[8];
|
||||||
|
Random rand = new Random();
|
||||||
|
rand.NextBytes(buf);
|
||||||
|
return BitConverter.ToInt64(buf, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,27 +1,73 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text;
|
using BlubbFish.Utils;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using BlubbFish.Utils.IoT.Bots;
|
using BlubbFish.Utils.IoT.Bots;
|
||||||
|
|
||||||
namespace Fraunhofer.Fit.IoT.LoraMap.Model.Admin {
|
namespace Fraunhofer.Fit.IoT.LoraMap.Model.Admin {
|
||||||
class AdminModel {
|
class AdminModel {
|
||||||
|
private readonly Dictionary<Int64, AdminSession> session = new Dictionary<Int64, AdminSession>();
|
||||||
public Boolean ParseReuqest(HttpListenerContext cont) {
|
public Boolean ParseReuqest(HttpListenerContext cont) {
|
||||||
//cont.Request.Url.PathAndQuery =
|
if(cont.Request.Url.PathAndQuery == "/admin/login") {
|
||||||
|
return this.Login(cont);
|
||||||
|
}
|
||||||
if(!this.CheckAuth(cont)) {
|
if(!this.CheckAuth(cont)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return Webserver.SendFileResponse(cont, "admin");
|
return Webserver.SendFileResponse(cont);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Boolean Login(HttpListenerContext cont) {
|
||||||
|
Dictionary<String, String> POST = Webserver.GetPostParams(cont.Request);
|
||||||
|
if(POST.ContainsKey("user") && POST["user"] == "admin" &&
|
||||||
|
POST.ContainsKey("pass") && POST["pass"] == "password") {
|
||||||
|
Int64 sessionid = 0;
|
||||||
|
while(true) {
|
||||||
|
sessionid = AdminSession.GetRandomSessionid();
|
||||||
|
if(!this.session.ContainsKey(sessionid)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(cont.Request.Cookies["loramapsession"] != null) {
|
||||||
|
if(Int64.TryParse(cont.Request.Cookies["loramapsession"].Value, out Int64 cookiesessionid)) {
|
||||||
|
if(this.session.ContainsKey(cookiesessionid)) {
|
||||||
|
if(!this.session[sessionid].IsLoggedin) {
|
||||||
|
sessionid = cookiesessionid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!this.session.ContainsKey(sessionid)) {
|
||||||
|
this.session.Add(sessionid, new AdminSession());
|
||||||
|
}
|
||||||
|
this.session[sessionid].IsLoggedin = true;
|
||||||
|
cont.Response.AppendCookie(new Cookie("loramapsession", sessionid.ToString()) {
|
||||||
|
Expires = DateTime.Now.AddYears(1)
|
||||||
|
});
|
||||||
|
cont.Response.AddHeader("Location", "/admin");
|
||||||
|
cont.Response.StatusCode = 307;
|
||||||
|
Console.WriteLine("200 - Login OK! " + cont.Request.Url.PathAndQuery);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
cont.Response.AddHeader("Location", "/admin/login.html");
|
||||||
|
cont.Response.StatusCode = 307;
|
||||||
|
Helper.WriteError("307 - Login WRONG! " + cont.Request.Url.PathAndQuery);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Boolean CheckAuth(HttpListenerContext cont) {
|
private Boolean CheckAuth(HttpListenerContext cont) {
|
||||||
if(cont.Request.Url.PathAndQuery.StartsWith("/admin/login")) {
|
if(cont.Request.Url.PathAndQuery.StartsWith("/admin/login.html")) {
|
||||||
return true;
|
return true;
|
||||||
} else if(cont.Request.Url.PathAndQuery.StartsWith("/admin/logout")) {
|
|
||||||
} else {
|
} else {
|
||||||
|
if(cont.Request.Cookies["loramapsession"] != null) {
|
||||||
|
if(Int64.TryParse(cont.Request.Cookies["loramapsession"].Value, out Int64 sessionid)) {
|
||||||
|
if(this.session.ContainsKey(sessionid)) {
|
||||||
|
return this.session[sessionid].IsLoggedin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
cont.Response.StatusCode = 403;
|
cont.Response.StatusCode = 403;
|
||||||
|
Helper.WriteError("403 - " + cont.Request.Url.PathAndQuery);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,7 @@ namespace Fraunhofer.Fit.IoT.LoraMap {
|
|||||||
cont.Response.StatusCode = 500;
|
cont.Response.StatusCode = 500;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return SendFileResponse(cont, "resources");
|
return SendFileResponse(cont);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
11
Lora-Map/resources/admin/index.html
Normal file
11
Lora-Map/resources/admin/index.html
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
|
||||||
|
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
ADMIN
|
||||||
|
</body>
|
||||||
|
</html>
|
15
Lora-Map/resources/admin/login.html
Normal file
15
Lora-Map/resources/admin/login.html
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
|
||||||
|
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form method="post" action="/admin/login">
|
||||||
|
U:<input name='user'><br>
|
||||||
|
P:<input type='password' name='pass'><br>
|
||||||
|
<input type='submit'>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user