asp.net 生成随机密码的代码

ASP.NET中的随机密码生成 第一种方法:   用.NET中的 Sytem.Guid.NewGuid().ToString() 生成,实际上是对一个128bit数字的十六进制输出.   生成的字符由0-9和a-z这几种字符组成,也可能会有“-”字符分割.   string strNewPW = System.Guid.NewGuid().ToString();   结果可能会是:7f44aed7-f8a4-4229-b64a-6a3e50d920e0   处理一下,去掉其中的“-”字符,剩下的是由32个阿拉伯数字和英文字母组成的串,再按自己的要求截取指定长度的字串.   截取8位:   string strN 立即下载

asp.net 生成随机密码。

代码:

public static class RandomPassword
   {
       // Define default min and max password lengths.
       private static int DEFAULT_MIN_PASSWORD_LENGTH = 8;
       private static int DEFAULT_MAX_PASSWORD_LENGTH = 10;

       // Define supported password characters divided into groups.
       private static string PASSWORD_CHARS_LCASE = "abcdefgijkmnopqrstwxyz";
       private static string PASSWORD_CHARS_UCASE = "ABCDEFGHJKLMNPQRSTWXYZ";
       private static string PASSWORD_CHARS_NUMERIC = "23456789";
       private static string PASSWORD_CHARS_SPECIAL = "*$-+?_&=!%{}/";

       /// <summary>
       /// Generates a random password.
       /// </summary>
       /// <returns>
       /// Randomly generated password.
       /// </returns>
       /// <remarks>
       /// The length of the generated password will be determined at
       /// random. It will be no shorter than the minimum default and
       /// no longer than maximum default.
       /// </remarks>
       public static string Generate()
       {
           return Generate(DEFAULT_MIN_PASSWORD_LENGTH,
                           DEFAULT_MAX_PASSWORD_LENGTH);
       }

       /// <summary>
       /// Generates a random password of the exact length.
       /// </summary>
       /// <param name="length">
       /// Exact password length.
       /// </param>
       /// <returns>
       /// Randomly generated password.
       /// </returns>
       public static string Generate(int length)
       {
           return Generate(length, length);
       }

       /// <summary>
       /// Generates a random password.
       /// </summary>
       /// <param name="minLength">
       /// Minimum password length.
       /// </param>
       /// <param name="maxLength">
       /// Maximum password length.
       /// </param>
       /// <returns>
       /// Randomly generated password.
       /// </returns>
       /// <remarks>
       /// The length of the generated password will be determined at
       /// random and it will fall with the range determined by the
       /// function parameters.
       /// </remarks>
       public static string Generate(int minLength,
                                     int maxLength)
       {
           // Make sure that input parameters are valid.
           if (minLength <= 0 || maxLength <= 0 || minLength > maxLength)
               return null;


           // Create a local array containing supported password characters
           // grouped by types. You can remove character groups from this
           // array, but doing so will weaken the password strength.
           char[][] charGroups = new char[][] 
       { www.jbxue.com
           PASSWORD_CHARS_LCASE.ToCharArray(),
           PASSWORD_CHARS_UCASE.ToCharArray(),
           PASSWORD_CHARS_NUMERIC.ToCharArray(),
           PASSWORD_CHARS_SPECIAL.ToCharArray()
       };

           // Use this array to track the number of unused characters in each
           // character group.
           int[] charsLeftInGroup = new int[charGroups.Length];


           // Initially, all characters in each group are not used.
           for (int i = 0; i < charsLeftInGroup.Length; i++)
               charsLeftInGroup[i] = charGroups[i].Length;


           // Use this array to track (iterate through) unused character groups.
           int[] leftGroupsOrder = new int[charGroups.Length];


           // Initially, all character groups are not used.
           for (int i = 0; i < leftGroupsOrder.Length; i++)
               leftGroupsOrder[i] = i;


           // Because we cannot use the default randomizer, which is based on the
           // current time (it will produce the same "random" number within a
           // second), we will use a random number generator to seed the
           // randomizer.


           // Use a 4-byte array to fill it with random bytes and convert it then
           // to an integer value.
           byte[] randomBytes = new byte[4];


           // Generate 4 random bytes.
           RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
           rng.GetBytes(randomBytes);


           // Convert 4 bytes into a 32-bit integer value.
           int seed = (randomBytes[0] & 0x7f) << 24 |
                       randomBytes[1] << 16 |
                       randomBytes[2] << 8 |
                       randomBytes[3];


           Random random = new Random(seed);
           char[] password = null;


           // Allocate appropriate memory for the password.
           if (minLength < maxLength)
               password = new char[random.Next(minLength, maxLength + 1)];
           else
               password = new char[minLength];


           // Index of the next character to be added to password.
           int nextCharIdx;


           // Index of the next character group to be processed.
           int nextGroupIdx;


           // Index which will be used to track not processed character groups.
           int nextLeftGroupsOrderIdx;


           // Index of the last non-processed character in a group.
           int lastCharIdx;


           // Index of the last non-processed group.
           int lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;


           // Generate password characters one at a time.
           for (int i = 0; i < password.Length; i++)
           {
               // If only one character group remained unprocessed, process it;
               // otherwise, pick a random character group from the unprocessed
               // group list. To allow a special character to appear in the
               // first position, increment the second parameter of the Next
               // function call by one, i.e. lastLeftGroupsOrderIdx + 1.
               if (lastLeftGroupsOrderIdx == 0)
                   nextLeftGroupsOrderIdx = 0;
               else
                   nextLeftGroupsOrderIdx = random.Next(0,
                                                        lastLeftGroupsOrderIdx);


               // Get the actual index of the character group, from which we will
               // pick the next character.
               nextGroupIdx = leftGroupsOrder[nextLeftGroupsOrderIdx];


               // Get the index of the last unprocessed characters in this group.
               lastCharIdx = charsLeftInGroup[nextGroupIdx] - 1;


               // If only one unprocessed character is left, pick it; otherwise,
               // get a random character from the unused character list.
               if (lastCharIdx == 0)
                   nextCharIdx = 0;
               else
                   nextCharIdx = random.Next(0, lastCharIdx + 1);


               // Add this character to the password.
               password[i] = charGroups[nextGroupIdx][nextCharIdx];


               // If we processed the last character in this group, start over.
               if (lastCharIdx == 0)
                   charsLeftInGroup[nextGroupIdx] =
                                             charGroups[nextGroupIdx].Length;
               // There are more unprocessed characters left.
               else
               {
                   // Swap processed character with the last unprocessed character
                   // so that we don't pick it until we process all characters in
                   // this group.
                   if (lastCharIdx != nextCharIdx)
                   {
                       char temp = charGroups[nextGroupIdx][lastCharIdx];
                       charGroups[nextGroupIdx][lastCharIdx] =
                                   charGroups[nextGroupIdx][nextCharIdx];
                       charGroups[nextGroupIdx][nextCharIdx] = temp;
                   }
                   // Decrement the number of unprocessed characters in
                   // this group.
                   charsLeftInGroup[nextGroupIdx]--;
               }


               // If we processed the last group, start all over.
               if (lastLeftGroupsOrderIdx == 0)
                   lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;
               // There are more unprocessed groups left.
               else
               {
                   // Swap processed group with the last unprocessed group
                   // so that we don't pick it until we process all groups.
                   if (lastLeftGroupsOrderIdx != nextLeftGroupsOrderIdx)
                   {
                       int temp = leftGroupsOrder[lastLeftGroupsOrderIdx];
                       leftGroupsOrder[lastLeftGroupsOrderIdx] =
                                   leftGroupsOrder[nextLeftGroupsOrderIdx];
                       leftGroupsOrder[nextLeftGroupsOrderIdx] = temp;
                   }
                   // Decrement the number of unprocessed groups.
                   lastLeftGroupsOrderIdx--;
               }
           }


           // Convert password characters into a string and return the result.
           return new string(password);
       }
   }

Python 爬虫实战:解析 JSON 数据爬取今日头条热点新闻(API 接口逆向与字段提取) 在当今信息爆炸的时代,新闻资讯的获取变得尤为重要。今日头条作为国内领先的新闻聚合平台,其热点新闻数据具有很高的价值。本文将详细介绍如何通过 Python 爬虫技术,逆向解析今日头条的 API 接口,提取并解析 JSON 数据,最终获取热点新闻信息。 阅读详情

相关推荐

centos7安装db2 version11.1

centos7安装db2 version11.1

AKA石头 1061

ASP.NET随机码生成示例

现在很多网页登陆的时候都使用了随机图片的方式,是一种简单、有效的防止黑客恶意攻击的手段。今天看了一些网上的资料,明白其生成原理:从样本中,获取随机字符串,随机字符串保存进session,并以位图的方式形成随机码图片。实现:添加命名空间 using System.Drawing;using System.Drawing.Imaging;using System.Drawing.Drawing2D;...

weixin_34159110的博客 177

Multisim14.3主数据库访问冲突:Windows更新KB5065426卸载与系统兼容性修复指南

本文针对Multisim14.3在Windows更新KB5065426后出现的主数据库访问冲突问题,提供了详细的解决方案。通过卸载问题更新、修复系统兼容性及调整权限设置,帮助用户快速恢复元器件库的正常访问,确保电路仿真设计的连续性。

lstm7chronicler的博客 511

ASP.net生成各种随机数字,用于优惠券、验证码等

北京网站建设-恒动时空 将网上查到的资料收集整理如下: 1、利用GUID生成全球唯一标识符,长度为32位,格式如:6F9619FF-8B86-D011-B42D-00C04FC964FF。     SQLSERVER本身可以生成,ASP.NET也可以生成。     最简单的如:GUID guid = Guid.newguid() 2、将GUID转为哈希数,然后随机     Random

xiaojie_cp的专栏 954

DOA估计算法从原理到仿真——CBF、Capon、MUSIC算法

从原理出发理解并实现经典的DOA估计算法

m0_55845774的博客 1万+

07 Zabbix 微信告警

Zabbix 微信告警 1.注册企业微信 企业微信注册地址:https://work.weixin.qq.com 2.进入企业微信后台管理 2.1 添加微信成员 2.2创建应用 2.3 获取四项数据 (1)AgentID (2) Secret (3) 企业ID (4)部门ID 3.Zabbix Server配置 3.1确认脚本目录 # grep alertscripts /etc/zabbix/zabbix_server....

Jian Sun_的博客 1297

ASP.NET 随机密码生成

QQ:285679784 欢迎加入博主优快云资源QQ群799473954(附加信息:优快云博客)一起学习 ! 前端aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CreateRandom.aspx.cs" Inherits="AspNetWebSocket.CreateRandom" Val...

ASP.NET 978

ESP32-C3入门教程 蓝牙篇①——基于Blufi 的 WiFi 智能配网

文章目录一、前言二、WiFi配网方式三、快速运行四、运行效果五、ESP32-C3程序流程5.1 主程序5.2 回调函数5.3 SmartConfig线程5.4 流程图六、关键函数6.1 设置SmartConfig的协议类型6.2 启动SmartConfig6.3 停止SmartConfig七、其他八、参考 一、前言 本文基于VS Code IDE进行编程、编译、下载、运行等操作 基础知识点请查阅:ESP32-C3入门教程 基础篇①——基于VS Code构建Hello World 二、WiFi配网方式 W

7764

asp.net 生成随机密码的具体代码

public static class RandomPassword { // Define default min and max password lengths. private static int DEFAULT_MIN_PASSWORD_LENGTH = 8; private static int DEFAULT_MAX_PASSWORD

unity3d,unity3d教程下载 560

Android 13 灭屏音乐播放问题解决与优化建议

通过在中增加对音乐播放状态的检测,我们成功解决了客户应用在灭屏状态下音乐播放被中断的问题。

m0_60898338的博客 1157

Linux C/C++ or 嵌入式面试之《C/C++笔面试系列》(10) C++ 前置后置增减运算符重载形式与比较

C语言的++、–运算符在此不讨论。 文章目录1、前置后置增减运算符重载的形式2、前置和后置运算符的性能比较 1、前置后置增减运算符重载的形式 自增运算符“++”和自减运算符“–”分别包含两个版本。即运算符前置形式(如 ++x)和运算符后置形式(如 x++),这两者进行的操作是不一样的。因此,当我们在对这两个运算符进行重载时,就必须区分前置和后置形式。C++根据参数的个数来区分前置和后置形式。如果按照通常的方法来重载 ++ 运算符(即作为成员函数重载时不带参数,或者作为非成员函数重载时带有一个参数),那么重载

二进制君 5831

浅析ASP.NET生成随机密码

在开发需要用户注册后才能使用提供的各项功能的应用程序时,在新用户提交注册信息后,较常见的做法是由程序生成随机密码,然后发送密码到用户注册时填写的电子信箱,用户再用收到的密码来激活其帐户。 实现ASP.NET生成随机密码功能是很容易的,下面的代码给出了完整的实现方法: public static string MakePassword(stringpwdchars,intpwdlen) {

caitianzhen2的专栏 323

Python爬虫实战:社交媒体热点事件追踪与情感分析可视化

本文探讨了如何利用Python爬虫技术从微博和Twitter等社交媒体平台抓取数据,进行热点事件追踪和情感分析,并通过可视化方式展示舆论情感变化。文章详细介绍了最新的爬虫技术、数据处理方法和可视化实现,提供了完整的代码示例,帮助读者构建自己的社交媒体分析系统。关键词:Python爬虫、社交媒体分析、情感分析、数据可视化、热点追踪爬虫框架:Requests-HTML、aiohttp、Twint(Twitter专用)数据处理文本处理:Jieba(中文分词)、NLTK(英文处理)情感分析。

2201_76125261的博客 1104

ASP.NET生成随机密码

<br />ASP.NET生成随机密码<br />在开发需要用户注册后才能使用提供的各项功能的应用程序时,在新用户提交注册信息后,较常见的做法是由程序生成随机密码,然后发送密码到用户注册时填写的电子信箱,用户再用收到的密码来激活其帐户。<br />实现ASP.NET生成随机密码功能是很容易的,下面的代码给出了完整的实现方法:<br />publicstaticstringMakePassword(stringpwdchars,intpwdlen)<br />{ <br />stringtmpstr="";

hhwydwfg的专栏 403

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

ASP.NET生成随机密码。

ASP.NET生成随机密码 在开发需要用户注册后才能使用提供的各项功能的应用程序时,在新用户提交注册信息后,较常见的做法是由程序生成随机密码,然后发送密码到用户注册时填写的电子信箱,用户再用收到的密码来激活其帐户。 实现ASP.NET生成随机密码功能是很容易的,下面的代码给出了完整的实现方法: publicstaticstringMakePassword(stringpwdchars,intpwdlen) { stringtmpstr=""; intiRandNum; Ran

ldneedu的专栏 653

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

ASP.NET 中的随机密码生成

  在开发需要用户注册后才能使用提供的各项功能的应用程序时,在新用户提交注册信息后,较常见的做法是由程序生成随机密码,然后发送密码到用户注册时填写的电子信箱,用户再用收到的密码来激活其帐户。   在 ASP.NET 中,实现随机密码生成功能是很容易的,下面的代码给出了完整的实现方法: public static string MakePassword(string pwdchars, int pw

★VS.Net编程★ 1375
上一篇: jquery获取子节点和父节点的例子
下一篇: 查看Oracle中是否有锁表的sql
my_baby_2009
博客等级 码龄14年 31粉丝 38原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值