匿名用户开启个性化设置[转载]

默认情况,ASP.NET2.0没有开启匿名用户个性化设置,通过Web.config设置,可以实现对匿名用户的支持。
    Web.config设置

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  
<appSettings/>
  
<connectionStrings/>
  
<system.web>
    
<anonymousIdentification enabled="true"/>
    
<profile>
      
<properties>
        
<add name="Name" allowAnonymous="true" />
        
<add name="LastSubmit" type="System.DateTime" allowAnonymous="true"/>
        
<group name="Address">
          
<add name="City"  allowAnonymous="true"/>
          
<add name="ZipCode"  allowAnonymous="true"/>
        
</group>
      
</properties>
    
</profile>
    
<compilation debug="true"/>
    
<authentication mode="Windows"/>
  
</system.web>
</configuration>


    anonymousIdentification:针对应用程序授权配置匿名标识。这是在需要授权时,对没有经过身份验证的实体进行标识所必需的。
    allowAnonymous必须设置为"True"。

   

Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!Page.IsPostBack)
        
{
            
//显示用户配置信息
            DisplayProfileInfo();
        }

    }

    
protected void btnSubmit_Click(object sender, EventArgs e)
    
{
        
//保存用户配置信息到Profile属性中
        Profile.Name = txtName.Text;
        Profile.Address.City 
= txtCity.Text;
        Profile.Address.ZipCode 
= txtPostalCode.Text;
        Profile.LastSubmit 
= DateTime.Now;
        
//显示用户配置信息
        DisplayProfileInfo();
    }

    
private void DisplayProfileInfo()
    
{
        
//从Profile属性中获取数据并赋值给服务器控件            
        txtName.Text = Profile.Name;
        txtCity.Text 
= Profile.Address.City;
        txtPostalCode.Text 
= Profile.Address.ZipCode;
        DateTime time 
= Profile.LastSubmit;
        
//如果未获取值则显示空,否则显示获取的值
        if (time.Year == 1)
        
{
            labLastSubmit.Text 
= "";
        }

        
else
        
{
            labLastSubmit.Text 
= time.ToString();
        }

    }

}

    以上代码出于《asp.net2.0开发指南》。

转载于:https://www.cnblogs.com/jRoger/articles/1833602.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值