简体   繁体   English

我如何为asp.net Web应用程序构建类体系结构

[英]How can i build my class architecture for my asp.net web application

I'm building a web application using .net 2010, c#. 我正在使用.net 2010 c#构建Web应用程序。 I want to create several projects in one solution. 我想在一个解决方案中创建多个项目。

  1. A asp.net web application 一个asp.net Web应用程序

  2. A class library to hold normal business logic. 用于保存常规业务逻辑的类库。

  3. A class library to hold business logic to call a third party API. 一个类库,用于保存业务逻辑以调用第三方API。

When i call third party API, I need to login first, and third party API will return a session for me to communicate until I logout. 当我调用第三方API时,我需要先登录,并且第三方API将返回一个会话供我通信,直到我注销为止。 What I want is to remain the same login session in asp.net web application. 我想要的是在asp.net Web应用程序中保留相同的登录会话。 Which means when user logged in from web form, the program will login third party API and grab a session for that user; 这意味着当用户从Web表单登录时,程序将登录第三方API并为该用户获取会话; another user logged in from web form, the program will login third party API and grab another session for that user. 从Web表单登录的另一个用户,程序将登录第三方API并为该用户获取另一个会话。 whenever the user make some calls to the business logic layer, the method in business logic layer should be able to know which session from third party API it is using. 每当用户对业务逻辑层进行一些调用时,业务逻辑层中的方法应该能够知道它正在使用第三方API中的哪个会话。

Also, I might want to reuse the 2 class library for a WPF app or a console app later. 另外,我以后可能想为WPF应用程序或控制台应用程序重用2类库。

How can I implement it? 我该如何实施?

Just save the session information of the 3rd party API acquired from the login within the Session object of your ASP.NET application and re-use it for subsequent requests to the API. 只需将从登录名获取的第三方API的会话信息保存在ASP.NET应用程序的Session对象中,然后将其用于后续对API的请求即可。 Ie assuming the 3rd party is providing a session cookie with the name .ASPXAUTH you could do the following: 也就是说,假设第三方正在提供名称为.ASPXAUTH的会话cookie,则可以执行以下操作:

        api.CookieContainer = new System.Net.CookieContainer();
        api.Login(user_name, password);
        Session["APIAuthenticationCookie"] = api.CookieContainer.GetCookies(new Uri(api.Url))[".ASPXAUTH"];


    //later request: reuse session cookie before using API
    api.CookieContainer = new CookieContainer(); 
    Cookie sessionCookie = (Cookie)Session["APIAuthenticationCookie"];
    if (sessionCookie != null)
        api.CookieContainer.Add(sessionCookie);
    api.RandomRequest();

If you might reuse the class libraries, and if one of those libraries is meant to abstract the access to the third-party API, then you should have a class in that library which is the entry point for the API. 如果您可能会重用类库,并且如果其中一个库旨在抽象化对第三方API的访问,那么您应该在该库中有一个类作为API的入口点。 It might include all of the methods of that API, or it might explicitly represent a "session" with that API. 它可能包含该API的所有方法,或者可能显式表示该API的“会话”。

This class should call the "login" method of the API as needed, and should hold a copy of the session key that the API returns. 此类应根据需要调用API的“登录”方法,并应保存API返回的会话密钥的副本。

In your ASP.NET application, you should keep an instance of this class in ASP.NET Session state. 在ASP.NET应用程序中,应将此类的实例保持在ASP.NET会话状态。 That way, there will be one per user. 这样,每个用户将只有一个。 In a Console or WPF application, just keep one instance around as a class member, perhaps as a static member. 在控制台或WPF应用程序中,只需将一个实例作为类成员(也许作为静态成员)保留在周围即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在本地iis上部署asp.net Web应用程序? - How can I deploy an asp.net web application on my local iis? 如何在Azure中的ASP.NET Web应用程序上调试502错误? - How can I debug 502 errors on my ASP.NET web application in Azure? 如何在asp.net 4.0中的整个Web应用程序中禁用浏览器的缓存? - How can I disable Browser's Cache throughout my web application in asp.net 4.0? 在我的 asp.net 核心 MVC web 应用程序中,我如何使验证码成为必填字段 - Inside my asp.net core MVC web application how i can make the recaptcha a required field 为我的asp.net mvc Web应用程序提供一个Repository类。 如何确保我正确处理所有东西 - Provide a Repository class for my asp.net mvc web application. How to make sure i am disposing eveything correctly 我的asp.net网络应用程序出现错误 - I get an error in my asp.net web application 我可以在.net 4中部署我的ASP.NET MVC 4应用程序吗? - Can I deploy my ASP.NET MVC 4 application in .net 4 这是设计我的asp.net应用程序架构的最佳方法 - which is the best approach in designing my asp.net application architecture 如何使用枚举中的值从ASP.NET MVC Web应用程序中的模型填充下拉列表? - How can I use values from an enum to populate a dropdownlist from my model in an ASP.NET MVC web application? 当托管在Azure应用程序服务中时,如何控制添加到ASP.NET核心Web应用程序中的HTTP响应标头? - How can I control the HTTP response headers added to my ASP.NET core web application when hosted in Azure app service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM