通过Web配置进行身份验证 [英] authentication through web config

查看:85
本文介绍了通过Web配置进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家有没有做过的示例代码,这些示例代码在web.config中声明了多个用户名,而使用它的后端c#代码呢?

关于

解决方案

如果我理解正确,那么您只是在Web.Config文件中定义了几个用户名密码作为键值对.
稍后,您选择支付的用户名密码的键值进行身份验证.现在,您的疑问在于如何在此流程中继续使用多个用户名密码.
如果是这样,
由于这是自定义的事情,因此您可以使用多种逻辑来保存和检索它.
1.保留一个密钥,并将所有用户名和密码括在方括号中.检索时将其拆分为后面的代码,然后进行比较以查看是否有任何匹配项.

 <  添加   密钥  ="      ="  {username1:password1},{username2:password2},{username3:password3},{username4:password4}"  <  添加     ="      ="  username1:password1" <  添加    ="   2:username:password"     ="  username2:password2" <  添加    ="   3:username:password"     ="  username3:password3" <  添加    ="   4:username:password"     ="  username4:password4" 了解ASP.NET角色和成员资格-入门教程 [ ^ ] 
了解并实现ASP.NET自定义表单身份验证 [ ^ ]

让我知道这些是否有助于解决您的问题.


无论如何....这是我用于Web配置的一种方法


<身份验证模式=表格">
 < forms loginUrl =〜/SignIn.aspx" name =.ASPXAUTH" slideExpiration ="true" timeout ="1440" path ="/" defaultUrl =〜/Default.aspx">
  < credentials passwordformat =清除">
   <用户名="testUser1"密码="testPass1"/>
   <用户名="testUser2"密码="testPass2"/>
   <用户名="testUser2"密码="testPass3"/>
  </credentials>
 </forms>
</authentication>




和登录后的代码

<pre lang="c#">if (FormsAuthentication.Authenticate(this.txtUserName.Text, this.txtPassword.Text))
 {
  FormsAuthentication.SetAuthCookie(this.txtUserName.Text, false);
  FormsAuthentication.RedirectFromLoginPage(this.txtUserName.Text, false);
 }
 else
 {
  Response.Write("Invalid login details. Please try again.");
 }


hi guys,does anyone have sample code that they have done that has multiple usernames declared in the web.config and backend c# code that uses it?

Regards

解决方案

If I understand correctly, you are just defining few username-passwords in your Web.Config file as a key-value pair.
Later, you pick the username-password''s key-value paid to authenticate. Now, your doubt is on how to proceed with multiple username-password in this flow.
If so,
Since, it''s a custom thing, you can multiple logics to keep and retrieve it.
1. Keep a single key with all the username, password in it enclosed in brackets. Split it in code behind at the time of retrieval and then compare to see if anything matches.

<add key="username:password" value="{username1:password1},{username2:password2},{username3:password3},{username4:password4}">



2. Keep multiple username-passwords together in your web.config and then retrieve all of them at the time of authentication. In your code behind, get the values of the known keys and store it that can be used later for comparison.

<add key="1:username:password" value="username1:password1">
<add key="2:username:password" value="username2:password2">
<add key="3:username:password" value="username3:password3">
<add key="4:username:password" value="username4:password4">




There can be various more ways, all you need is to stick with any one and just code to get the pairs in code behind. Use the pairs for comparison.
Keep it in an application variable or cache it to not retrieve again and again.


I didn''t understood the question completely but I think These two article talks about this in details. you will also find sample projects to get a look at the code.

Understanding ASP.NET Roles and Membership - A Beginner''s Tutorial[^]
Understanding and Implementing ASP.NET Custom Forms Authentication[^]

Let me know if these help in solving your issue.


anyway....this is a method i used for the web config


<authentication mode="Forms">
 <forms loginUrl="~/SignIn.aspx" name=".ASPXAUTH" slidingExpiration="true" timeout="1440" path="/" defaultUrl="~/Default.aspx">
  <credentials passwordformat="Clear">
   <user name="testUser1" password="testPass1" />
   <user name="testUser2" password="testPass2" />
   <user name="testUser2" password="testPass3" />
  </credentials>
 </forms>
</authentication>




and code behind the login

<pre lang="c#">if (FormsAuthentication.Authenticate(this.txtUserName.Text, this.txtPassword.Text))
 {
  FormsAuthentication.SetAuthCookie(this.txtUserName.Text, false);
  FormsAuthentication.RedirectFromLoginPage(this.txtUserName.Text, false);
 }
 else
 {
  Response.Write("Invalid login details. Please try again.");
 }


这篇关于通过Web配置进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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