这个问题之所以存在,是因为它确实存在 具有历史意义,但事实并非如此 被认为是一个很好的主题问题 因为是本网站,所以请不要使用 作为证据,你可以问类似的问题 这里的问题。 更多信息:https://stackoverflow.com/faq


总有一些功能在边缘场景中很有用,但正是因为这个原因,大多数人都不知道它们。我要求的是课本上通常没有教过的特性。

你知道的是什么?


当前回答

If you use web services instead WCF services, you can still use standard .Net membership to enforce authentication and login session behaviour on a set web services similarly to a how you would secure web site with membership forms authentication & without the need for a special session and/or soap headers implementations by simply calling System.Web.Security.FormsAuthentication.SetAuthCookie(userName, false) [after calling Membership.ValidateUser(userName, password) of course] to create cookie in the response as if the user has logged in via a web form. Then you can retrieve this authentication cookie with Response.Cookies[].Value and return it as a string to the user which can be used to authenticate the user in subsequent calls by re-creating the cookie in the Application_BeginRequest by extracting the cookie method call param from the Request.InputStream and re-creating the auth cookie before the membership authenticates the request this way the membership provider gets tricked and will know the request is authenticated and enforce all its rules.

将此cookie返回给用户的示例web方法签名如下: 字符串登录(用户名、密码)

后续web方法调用示例如下: 字符串DoSomething(字符串authcookie,字符串methodParam1,int methodParam2等,等),你需要提取authcookie(这是从登录方法获得的值)参数从请求。InputStreamis

这也模拟了一个登录会话并调用FormsAuthentication。签出在web方法,如注销(authcookie)将 使用户需要再次登录。

其他回答

这似乎是一个巨大而模糊的问题…… 但我将在这里介绍Reflection,因为它允许我做一些非常强大的事情,如可插拔的DALs等。

你可以使用ASP。.aspx页面中的。NET注释用于注释掉页面的全部部分,包括服务器控件。而且被注释掉的内容永远不会被发送到客户端。

<%--
    <div>
        <asp:Button runat="server" id="btnOne"/>
    </div>
--%>

默认情况下,任何web表单页面都继承自System.Web.UI.Page类。如果您希望您的页面继承自自定义基类,继承自System.Web.UI.Page怎么办?

有一种方法可以约束任何页面从您自己的基类继承。只需在web.config中添加一行:

<system.web>
    <pages pageBaseType="MyBasePageClass" />
</system.web>

注意:只有当你的类是一个独立的类时,这才有效。我的意思是一个没有隐藏代码的类,它看起来像<%@ Page Language=" c# " AutoEventWireup="true" %>

Page指令中的MaintainScrollPositionOnPostback属性。它用于在回发时保持aspx页面的滚动位置。

模板化用户控件。一旦你知道它们是如何工作的,你就会看到各种各样的可能性。下面是最简单的实现:

TemplatedControl.ascx

这里最棒的事情是使用简单而熟悉的用户控件构建块,并能够使用HTML和一些占位符来布局UI的不同部分。

<%@ Control Language="C#" CodeFile="TemplatedControl.ascx.cs" Inherits="TemplatedControl" %>

<div class="header">
    <asp:PlaceHolder ID="HeaderPlaceHolder" runat="server" />
</div>
<div class="body">
    <asp:PlaceHolder ID="BodyPlaceHolder" runat="server" />
</div>

TemplatedControl.ascx.cs

这里的“秘密”是使用ITemplate类型的公共属性,并知道[ParseChildren]和[PersistenceMode]属性。

using System.Web.UI;

[ParseChildren(true)]
public partial class TemplatedControl : System.Web.UI.UserControl
{
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate Header { get; set; }

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate Body { get; set; }

    void Page_Init()
    {
        if (Header != null)
            Header.InstantiateIn(HeaderPlaceHolder);

        if (Body != null)
            Body.InstantiateIn(BodyPlaceHolder);
    }
}

default . aspx

<%@ Register TagPrefix="uc" TagName="TemplatedControl" Src="TemplatedControl.ascx" %>

<uc:TemplatedControl runat="server">
    <Header>Lorem ipsum</Header>
    <Body>
        // You can add literal text, HTML and server controls to the templates
        <p>Hello <asp:Label runat="server" Text="world" />!</p>
    </Body>
</uc:TemplatedControl>

你甚至会得到内部模板属性的智能感知。因此,如果您在团队中工作,您可以快速创建可重用的UI,以实现与您的团队已经从内置ASP中享受到的相同的可组合性。NET服务器控件。

MSDN示例(与开头的链接相同)添加了一些额外的控件和命名容器,但只有当您希望支持“中继器类型”控件时,才需要这样做。