--------------------------------------------------------------------------------------------------------
First You Add This Code in Class File
------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ModelName.Models
{
public class Encryptiondecryption
{
public string Decode(string sData)
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(sData);
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
return result;
}
public string Encode(string sData)
{
try
{
byte[] encData_byte = new byte[sData.Length];
encData_byte = System.Text.Encoding.UTF8.GetBytes(sData);
string encodedData = Convert.ToBase64String(encData_byte);
return encodedData;
}
catch (Exception ex)
{
throw new Exception("Error in base64Encode" + ex.Message);
}
}
}
}
-----------------------------------------------------------------------------------------------------------
Then go to the Admin Controlller Apply This Code
Firstly Set The TextBox Name and Etc another Names
-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models Name;
namespace Controller Name
{
public class AdminController : Controller
{
//
// GET: /Admin/
public ActionResult Index()
{
return View();
}
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Http_login(string username, string password)
{
YourEntityName ObjEntity = new YourEntityName();
var Admindata = (from p in ObjEntity.tbl_User where p.username == username select p).SingleOrDefault();
if (Admindata!= null)
{
Encryptiondecryption Obj_Encryptiondecryption = new Encryptiondecryption();
string decdrypt_password = Obj_Encryptiondecryption.Decode(Admindata.password);
if (decdrypt_password == password)
{
Session["User"] = Admindata.username;
return RedirectToAction("Index", "ShareYourStory");
}
else
{
ModelState.AddModelError("Msgpsd", "Wrong Password!");
return View("Login");
}
}
else
{
ModelState.AddModelError("Msgusr", "Wrong Usename!");
return View("Login");
}
}
// Change Password of admin
public ActionResult Change_Password()
{
if (Session["User"] == null)
{
return RedirectToAction("Login", "Admin");
}
else
{
if (TempData["success"] == null)
{
TempData["success"] = "";
return View();
}
else
{
return View();
}
}
}
// Change Password of admin Http_Post
[HttpPost]
public ActionResult Http_ChangePassword( string txt_cnf_new_password, string txt_old_password)
{
EntityName obj_YourEntities = new EntityNameEntities();
string username = Session["user"].ToString();
var admindata = (from data in obj_YourEntities.tbl_User where data.username ==username select data).SingleOrDefault();
if (admindata != null)
{
Encryptiondecryption Objencryptcls = new Encryptiondecryption();
string encrypt_old_password=Objencryptcls.Decode(admindata.password);
if (encrypt_old_password == txt_old_password)
{
YourEntityName obj_YourEntities1 = newYourEntityName();
tbl_User objUser = new tbl_User();
//Encryptiondecryption Objencryptcls = new Encryptiondecryption();
string encrypt_pass = Objencryptcls.Encode(txt_cnf_new_password);
objUser.password = encrypt_pass;
objUser.username = username;
objUser.userid = 1;
obj_YourEntities1.tbl_User.Attach(objUser);
obj_YourEntities1.ObjectStateManager.ChangeObjectState(objUser, System.Data.EntityState.Modified);
obj_YourEntities1.SaveChanges();
TempData["success"] = "Password changed successfully";
return View("Change_Password");
}
else
{
ModelState.AddModelError("Msgpsd", "Wrong Password!");
return View("Change_Password");
}
}
else
{
ModelState.AddModelError("Msgusr", "Wrong Usename!");
return View("Change_Password");
}
}
// action for logoff admin
[HttpPost]
public ActionResult logoff()
{
Session["User"] = null;
return RedirectToAction("Login", "Admin");
}
}
}
First You Add This Code in Class File
------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ModelName.Models
{
public class Encryptiondecryption
{
public string Decode(string sData)
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(sData);
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
return result;
}
public string Encode(string sData)
{
try
{
byte[] encData_byte = new byte[sData.Length];
encData_byte = System.Text.Encoding.UTF8.GetBytes(sData);
string encodedData = Convert.ToBase64String(encData_byte);
return encodedData;
}
catch (Exception ex)
{
throw new Exception("Error in base64Encode" + ex.Message);
}
}
}
}
-----------------------------------------------------------------------------------------------------------
Then go to the Admin Controlller Apply This Code
Firstly Set The TextBox Name and Etc another Names
-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models Name;
namespace Controller Name
{
public class AdminController : Controller
{
//
// GET: /Admin/
public ActionResult Index()
{
return View();
}
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Http_login(string username, string password)
{
YourEntityName ObjEntity = new YourEntityName();
var Admindata = (from p in ObjEntity.tbl_User where p.username == username select p).SingleOrDefault();
if (Admindata!= null)
{
Encryptiondecryption Obj_Encryptiondecryption = new Encryptiondecryption();
string decdrypt_password = Obj_Encryptiondecryption.Decode(Admindata.password);
if (decdrypt_password == password)
{
Session["User"] = Admindata.username;
return RedirectToAction("Index", "ShareYourStory");
}
else
{
ModelState.AddModelError("Msgpsd", "Wrong Password!");
return View("Login");
}
}
else
{
ModelState.AddModelError("Msgusr", "Wrong Usename!");
return View("Login");
}
}
// Change Password of admin
public ActionResult Change_Password()
{
if (Session["User"] == null)
{
return RedirectToAction("Login", "Admin");
}
else
{
if (TempData["success"] == null)
{
TempData["success"] = "";
return View();
}
else
{
return View();
}
}
}
// Change Password of admin Http_Post
[HttpPost]
public ActionResult Http_ChangePassword( string txt_cnf_new_password, string txt_old_password)
{
EntityName obj_YourEntities = new EntityNameEntities();
string username = Session["user"].ToString();
var admindata = (from data in obj_YourEntities.tbl_User where data.username ==username select data).SingleOrDefault();
if (admindata != null)
{
Encryptiondecryption Objencryptcls = new Encryptiondecryption();
string encrypt_old_password=Objencryptcls.Decode(admindata.password);
if (encrypt_old_password == txt_old_password)
{
YourEntityName obj_YourEntities1 = newYourEntityName();
tbl_User objUser = new tbl_User();
//Encryptiondecryption Objencryptcls = new Encryptiondecryption();
string encrypt_pass = Objencryptcls.Encode(txt_cnf_new_password);
objUser.password = encrypt_pass;
objUser.username = username;
objUser.userid = 1;
obj_YourEntities1.tbl_User.Attach(objUser);
obj_YourEntities1.ObjectStateManager.ChangeObjectState(objUser, System.Data.EntityState.Modified);
obj_YourEntities1.SaveChanges();
TempData["success"] = "Password changed successfully";
return View("Change_Password");
}
else
{
ModelState.AddModelError("Msgpsd", "Wrong Password!");
return View("Change_Password");
}
}
else
{
ModelState.AddModelError("Msgusr", "Wrong Usename!");
return View("Change_Password");
}
}
// action for logoff admin
[HttpPost]
public ActionResult logoff()
{
Session["User"] = null;
return RedirectToAction("Login", "Admin");
}
}
}
No comments:
Post a Comment