您有两个 ASP.NET Web 应用程序,其中一个应用程序只对用户进行身份验证?
这听起来像是……的工作
网络服务!
在身份验证应用程序上创建一个新的 Web 服务(它们是 .asmx 扩展名),并添加一个接受用户和密码等的方法,并返回身份验证信息。
然后在您的第二个应用程序上导入 WSDL,并像调用方法一样调用第一个应用程序。它将简化您的代码并解决您的问题。
一个例子:
AuthenticateUserService.asmx 继续 Authentication 应用程序:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class AuthenticateUserService : System.Web.Services.WebService
{
[WebMethod]
public bool AuthenticateUser(string username, string passhash)
{
// Fake authentication for the example
return (username == "jon" && passhash == "SomeHashedValueOfFoobar");
}
}
设置完成后,启动您的主应用程序,然后右键单击项目并单击“添加 Web 引用”。
在身份验证应用上输入 asmx 的 url,Visual Studio 将发现它并创建一个代理类。
完成后,我们可以像调用主应用程序中的本地方法一样调用该方法:
protected void Page_Load(object sender, EventArgs e)
{
// Now we can easily authenticate user in our code
AuthenticateUserService authenticationProxy =
new AuthenticateUserService();
bool isUserAuthenticated =
authenticationProxy.AuthenticateUser("jon", SomeHashMethod("foobar"));
}
那么,这到底有什么作用呢?
它将客户端从身份验证过程中消除。
您当前的流程:
- 客户端向 AppA 输入凭据
- AppA 将客户端重定向到 AppB
- 如果凭据匹配,AppB 会将客户端重定向回 AppA。
替换为 AppA 和 AppB 之间的服务器端 SOAP 调用。现在是这样的:
- 客户端在 AppA 中输入凭据
- AppA 询问 AppB 是否良好
- AppA 向客户端提供适当的内容。