如何在asp.net mvc的加密? [英] how to make encrypt in asp.net mvc?

查看:398
本文介绍了如何在asp.net mvc的加密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了解在asp.net MVC加密

它是如此难以了解一下吧。

能有人给我简单的例子在asp.NET MVC ??

要使用加密

我尽量让在asp.net的MVC简单的应用程序使用加密。我读了一些文章引用有关加密。但没有一篇文章显示如何使用asp.net mvc的数据或查询加密3或4

 能有人给我关于加密在asp.net mvc的参考文献3/4或给我加密的一些简单的例子..


解决方案

这里是你可以用它来加密和解密您的网址库
http://jbtule.github.io/keyczar-dotnet/

给您的使用情况

,你要创建encryted网址和电子邮件发送

对于这个答案的缘故,让pretend的已加密的URL是

yourdomainname /加密/ kladsfiopfvnjladsiopdfsipejkadsjsadipodafskl

假设加密的字符串重新presents可以解密到田间地头用户名,dateValidThrough和EMAILADDRESS,由分隔的一个字符串| charachters。即

用户名+| + datevalidthough +| EMAILADDRESS

好吧,所以收到加密的URL,你有一个路线在RouteConfig.cs路线

  routes.MapRoute(
            名称:PaswordReset
            网址:加密/(编号),
            默认:新{控制器=密码,行动=复位,ID = UrlParameter.Optional});

这将带他们到该位指示有两个方法

 公共类PasswordController {  公众的ActionResult复位(字符串ID){}
    //返回具有形式他们输入用户名和电子邮件视图  [HttpPost]
  公众的ActionResult复位(字符串ID,用户名字符串,字符串email){}
   //这里derypt和验证等。 }

首先,他们将显示,使他们能够进入他们的电子邮件地址和用户名的形式。它应该有一个隐藏字段的enrypted URL部分

当他们提交表单,使用您与它的加密口令解密ID。

验证他们的用户名和电子邮件地址匹配起来。确保datevalidthrough为<今天,然后让他们改变自己的密码。

为了使加密的字符串,你可以使用上面的库,它会像

 字符串明文=用户名+| + DateTime.Today.AddDays(2)的ToString()+| + userEmailAddress;
WebBase64密文;
字符串键=(新的随机()随机(10000000)+ 1000)的ToString();// pretend本字典重新presents数据库存储介质
公共静态字典<字符串,字符串> CypherTextToKey =新词典<字符串,字符串>();//加密
使用(VAR加密器=新的加密器(密钥))
{
   密文= encrypter.Encrypt(明文);
}
CypherTextToKey.Add(密文,密钥);
SendEmail(用户,加密/+密文);

要解密它,你会做这样的事情。

  VAR键= CypherTextToKey [密文] //从数据库中的关键
使用(VAR crypter =新Crypter(键)){
  VAR plaintext2 = crypter.Decrypt(密文)
}

如果所有的比赛,给他们一个新的密码等。

即使世界的方式来操作使用加密的URL整个网站,但多数民众赞成一个不同的故事,我不知道如果这就是你所需要的(更复杂的一个位)。如果要加密每个URL进行评论,我会尽快给你另一个答案。

i try to learn about encrypt in asp.net MVC

it's so hard to understand about it..

can some one give me simple example to use encrypt in asp.NET MVC??

i try to make simple application use encrypt in asp.net mvc. i read some article to reference about encrypt. but no one article show how to encrypt data or query with asp.net mvc 3 or 4

can some one give me reference about encrypt in asp.net mvc 3 / 4 or give me some simple example of encrypt..

解决方案

here is library you can use to encrypt and decrypt your urls http://jbtule.github.io/keyczar-dotnet/

given your use case, you are going to create the encryted url and send it in an email

for the sake of this answer, lets pretend the encypted url is

"yourdomainname/encrypted/kladsfiopfvnjladsiopdfsipejkadsjsadipodafskl"

suppose the encrypted string represents the a string which can be decrypted to the fields username, dateValidThrough, and emailaddress, seperated by "|" charachters. ie

username + "|" + datevalidthough + "|" emailaddress

okay, so to recieve the encrypted url, you have a route in your RouteConfig.cs routes

        routes.MapRoute(
            name: "PaswordReset",
            url: "Encrypted/{id}",
            defaults: new {controller = "Password", action = "Reset", id = UrlParameter.Optional});       

this would take them to this contoller which has two methods

public class PasswordController{

  public ActionResult Reset(string id){}
    //returns a view that has a form for them to enter their username and email

  [HttpPost]
  public ActionResult Reset(string id, string username, string email){}
   //derypt here and verify etc.

 }

First, they would be shown a form in which they can enter their email address and username. it should have a hidden field for the enrypted url part

When they submit the form, decrypt the id using the passphrase that you encrypted it with.

Verify that their username and email address match up. Make sure the datevalidthrough is < today, then allow them to change their password.

To make the encrypted string, you would use the library above and it would be like

string plaintext = userName + "|" + DateTime.Today.AddDays(2).ToString() + "|" + userEmailAddress;
WebBase64 ciphertext;
string key = (new Random().Random(10000000) + 1000).ToString();

//pretend this dictionary represents a database storage medium
public static Dictionary<string,string> CypherTextToKey= new Dictionary<string,string>();

//encrypting
using (var encrypter = new Encrypter(key))  
{
   ciphertext = encrypter.Encrypt(plaintext);
}
CypherTextToKey.Add(cyphertext, key);
SendEmail(user, "Encrypted/"+cyphertext");

To decrypt it you would do something like

var key = CypherTextToKey[cyphertext]; //get the key from your database
using (var crypter = new Crypter(key)){
  var plaintext2 = crypter.Decrypt(ciphertext)
}

if all matches, give them a new password, etc.

Theres a way to operate an entire site using encrypted urls, but thats a different story and I'm not sure if thats what you need (its a bit more complicated). If you want to encrypt every url make a comment and I'll get back to you with another answer.

这篇关于如何在asp.net mvc的加密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆