欢迎光临
我们一直在努力

asp.net 学习笔记-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

 

用wsdl命令可以注册web service
在aps.net中创建web服务
以.asmx扩展名保存文件
<%@ webservice language=”c#” class=”testws” %>
using system.web.services;
class testws
{
 [webmethod]
 public string sayhello(string name)
 {
  return “hello”+name;
 }
}

post 调用web service
//以下为a.html文件内容
<form name=”f1″ method=”post” action=”http://locallhost/webservicetest/service1.asmx/helloworld”>
 <input type=”test” name=”name”><input type=”submit”>
</form>

get 调用web service
在url中传参
如:
http://localhost/webservicetest/service1.asmx?op=helloworld&name=myname

将apsx页面修改为用户控件
去除<html> <body> <form>元素
将web窗体页中asp.net指令类型从@page更改为@control
更改指令的codebehind属性引用以反映.aspx扩展名将更改为 .ascx
将基类从system.web.ui.page更改为system.web.ui.usercontrol

在用户控件中,控件的值可以定义属性
有一个用户控件,如果无法访问的话,可以用findcontrol方法
变量=((testcontrol)this.findcontrol(“tc”)).txtusername;
//response.write(((testcontrol)this.findcontrol(“tc”)).txtusername);
(testcontrol)是强制类型转换,括号内是类型
findcontrol(“tc”) tc是控件的name
.txtusername是控件的属性

用户控件的使用(在apsx页面中注册)
<%@ register tagprefix=”uc1″ tagname=”menu” src=”menu.ascx” %>
tagprefix 确定用户控件的唯一命名空间,它将是标记中控件名称的前缀
tagname 为用户控件的名称
src 用户控件的虚拟路径,例如”usercontrol1.ascx”
web自定义控件

web.config
<!–
 说明:
 1.所有的配置都必须被放在<configuration>和</configuration>标记之中.
 2.<appsettings>和</appsettings>之间是自定义配置,通常用来自己设置一些常量,add添加常量,key是常量的名称,
   value是常量的值.
   <appsettings>
     <add key=”con” value=”server=.;database=northwind;uid=sa;pwd=;”></add>
   </appsettings>
在程序中可以用system.configuration.configurationsettings.appsettings[“con”]取值
  sqlconnection con=new sqlconnection(system.configuration.configurationsettings.appsettings[“con”]);
  con.open();
  sqlcommand cmd=new sqlcommand(“select * from employees”,con);
  this.datagrid1.datasource=cmd.executereader();
  this.datagrid1.databind();

 3.<system.web>和</system.web>之间的标记是关于整个应用程序的设置.
  如 <pages buffer=”true”/> 使用页缓冲
 4.<location>和</location>是一个区域标记.path=”aaa”表示下面的设置只对该文件有效.
–>

customerrors设置(在<system.web>和</system.web>之间)
语法
<customerrors
  defaultredirect=”url”
  mode=”on|off|remoteonly”>
  <error statuscode=”statuscode” redirect=”url”/>
</customerrors>
身份验证和授权
身份验证类型: windows 描述: windows 身份难作为默认的身份验证模式.用于任何形式的iis身份验证
身份验证类型: forms 描述: 基于aps.net窗体的身份验证作为默认的身份验证模式
身份验证类型: passport 描述:microsoft passport身份验证作为默认的身份验证模式
身份验证类型: none 描述: 没有身份验证.用于匿名用户和可以提供其自己的身份验证的应用程序.
<configuration>
 <system.web>
  <authentication mode=”windows|forms|passport|none”>?
    <forms name=”name” loginurl=”url”
      protection=”all|none|encryption”
      timeout=”xx” path=”/”>?
       <credentials passwordformat=”clear|sha1|md5″> /*clear为明文密码*/
        <user name=”用户名” password=”密码”/>
       </credentials>
      </forms>?
     <passport redirecturl=”internal”/>?
    </authentication>
 </system.web>
</configuration>

//基于forms先把iis中该应用的匿名访问去掉
<forms>标记的属性
属性      选项          描述
name       none        登录身份验证的cookie名称
loginurl   none        登录页url.如果没有身份验证cookie,客户端将被重定向到此url
protection all         应用程序同时使用数据验证和加密来保护cookie
           none        加密和验证都禁用
timeout                一段时间(按分钟计),这段时间之后身份验证cookie将到期,默认值为30
path                   由应用程序发布的cookie的路径.默认值是反斜杠(/)

<authentication mode=”forms”>
 <forms name=”yourcookiename” loginurl=”login.aspx” protection=”all”></forms>
</authentication>
//授权
<authorization>
 <allow users=”?”/>        //<allow users=”*”/><!–允许所有用户 –>
  <!–
   <allow users=”[逗号分隔的用户列表]”
          roles=”[逗号分隔的角色列表]”/>
   <deny users=”[逗号分隔的用户列表]”
          roles=”[逗号分隔的角色列表]”/>
 –>
</authorization>

//login.aspx
登录代码
//连接数据库进行验证
if(true)//用户名是否合法
{
// system.web.security.formsauthentication.setauthcookie(this.textbox1.text,false);//指定的用户名写入到cookie中(false临时cookie,true永久cookie)
// response.redirect(“”);
 system.web.security.formsauthentication.redirectfromloginpage(this.textbox1.text,false);//转到用户原访问的页
//如果为true,可用system..web.security.formsauthentication.signout();删除,以后又要求登录
}
else
{
 response.write(“用户不合法”);
}

//如果用户少的话,可以不用数据库,直接允许/拒绝用户
<authentication mode=”forms”
 <forms name=”authcre” loginurl=”login.aspx” protection=”all”>
  <credentials passwordformat=”clear”>
   <user name=”aaa” password=”aaa”/>
   <user name=”bbb” password=”bbb”/>
  </credentials>
 </forms>
</authentication>
 登录代码
private void button1_click(object sender,system.eventargs e)
{
 if(system.web.security.formsauthentication.authenticate(this.textbox1.text,this.textbox2.text)
 {
//   system.web.security.formsauthentication.setauthcookie(this.textbox1.text,true);//此方法需重定向
//   response.redirect(“webform1.aspx”);
   system.web.security.formsauthentication.redirectfromloginpage(this.textbox1.text,false);//此方法不需要重定向,直接转到原访问页
 }
 else
 {
  response.write(“用户不合法”);
 }
}
//授权时,通配符*表示任何人,?表示匿名

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » asp.net 学习笔记-.NET教程,Asp.Net开发
分享到: 更多 (0)