简体   繁体   English

MVC5 可移植区,你如何使用消息总线?

[英]MVC5 Portable Area, how do you use message bus?

I spent several hours reading through many different examples and documentation for setting up a portable area with ASP.NET MVC, with the intent of sharing a common login page with authentication for multiple applications.我花了几个小时阅读了许多不同的示例和文档,以使用 ASP.NET MVC 设置可移植区域,目的是共享一个具有多个应用程序身份验证的公共登录页面。 I got it all together and it works nicely so far, but one thing I'm having trouble with is the use of message bus.我把它放在一起,到目前为止它运行良好,但我遇到的一件事是使用消息总线。 I see it is a way of communicating between the Host and Portable components, but I don't see a clear way of how to do this.我认为这是 Host 和 Portable 组件之间的一种通信方式,但我没有看到如何做到这一点的明确方法。

For instance;例如; if my Portable login is successful, how do I tell the Host so it can do something (set a cookie, redirect to a specific page, etc.)?如果我的便携式登录成功,我如何告诉主机以便它可以执行某些操作(设置 cookie、重定向到特定页面等)? Also, if I want to send something to the Portable (like the title or Assembly Version of the Host application) how would I do that?另外,如果我想向 Portable 发送某些内容(例如主机应用程序的标题或程序集版本),我该怎么做? I haven't tried anything yet because I cannot seem to find a complete example.我还没有尝试过任何东西,因为我似乎找不到一个完整的例子。

I got it all figured out.我都想通了。 There was a MvcContrib source code archive that I was unable to download since Google Chrome was blocking the .zip file, but I was able to get it using Internet Explorer.由于 Google Chrome 阻止了 .zip 文件,我无法下载MvcContrib 源代码存档,但我可以使用 Internet Explorer 获取它。

Here are the important bits after adapting it to my application.以下是将其调整到我的应用程序后的重要部分。 Hopefully this can help someone.希望这可以帮助某人。 I did my best to format my answer properly, this is my first time posting on StackOverflow:我尽力正确格式化我的答案,这是我第一次在 StackOverflow 上发帖:

In the Portable class library在可移植类库中

  1. Create a LoginResult class using the ICommandResult interface使用 ICommandResult 接口创建 LoginResult 类

    public class LoginResult: MvcContrib.PortableAreas.ICommandResult { public bool Success { get; set; } public string Message { get; set; } public string Username { get; set; } }
  2. Create a LoginMessage class that also uses the ICommandResult interface with LoginResult.创建一个 LoginMessage 类,该类也将 ICommandResult 接口与 LoginResult 一起使用。 The LoginMessage class has a property for a LoginViewModel I use in my Login.cshtml view (it has Username, Password, and some other additional fields I needed for the view) LoginMessage 类具有我在 Login.cshtml 视图中使用的 LoginViewModel 的属性(它具有用户名、密码和视图所需的其他一些其他字段)

     public class LoginMessage : ICommandMessage<LoginResult> { public LoginResult Result { get; set; } public LoginViewModel Input { get; set; } }
  3. In the HttpPost action of the Login controller, create an instance of LoginMessage, passing in the LoginViewModel from the login view, and send it to the Host with MvcContrib.Bus.Send在Login控制器的HttpPost动作中,创建LoginMessage的一个实例,从登录视图传入LoginViewModel,用MvcContrib.Bus.Send发送给Host

     [HttpPost] public ActionResult Login(LoginViewModel mdl) { // TODO: Do basic auth here first, then send to Host for additional validation // Create and send message to the Host var message = new LoginMessage { Input = mdl, Result = new LoginResult() }; MvcContrib.Bus.Send(message); if (message.Result.Success) { // Redirect to defaultUrl set in the Host's web.config FormsAuthentication.RedirectFromLoginPage(mdl.Username, false); } return View("Login", "_Layout", mdl); }

Note: LoginMessage sets a new empty LoginResult and then waits for Success.注意:LoginMessage 设置一个新的空 LoginResult,然后等待 Success。 The Success is set by the Host (shown below).成功由主机设置(如下所示)。 I do this because certain Host applications have specific additional logic that only apply to that application, so I let the Host do what it needs and return to the Portable to let it know if it passed or failed.我这样做是因为某些 Host 应用程序具有仅适用于该应用程序的特定附加逻辑,所以我让 Host 做它需要的事情并返回到 Portable 让它知道它是通过还是失败。 Eventually, I will have the basic authentication logic in the Portable first and then let the Host do the extra work, but for the sake of this example I am keeping it simple.最终,我将首先在 Portable 中拥有基本的身份验证逻辑,然后让 Host 做额外的工作,但为了这个例子,我保持简单。

In the Host web application (which has a reference to my Portable dll)在 Host Web 应用程序中(其中引用了我的 Portable dll)

  1. Create a handler for the Portable.LoginMessage so we can read it in the Host.为 Portable.LoginMessage 创建一个处理程序,以便我们可以在主机中读取它。 Note that IsValidLogin is where I will eventually do my additional authentication logic to see if the user is valid请注意 IsValidLogin 是我最终将执行额外身份验证逻辑以查看用户是否有效的地方

    public class LoginHandler : MvcContrib.PortableAreas.MessageHandler<Portable.LoginMessage> { public override void Handle(Portable.LoginMessage message) { if (IsValidLogin(message.Input.Username, message.Input.Password)) { message.Result.Success = true; message.Result.Username = message.Input.Username; } else { message.Result.Message = "Username or Password was incorrect"; } } private bool IsValidLogin(string username, string password) { // TODO: Replace with actual authentication return username.Equals("admin") && password.Equals("password"); } }
  2. In the web.config, set the defaultUrl that the Portable will redirect to in the controller I described earlier, when message.Result.Success is True.在 web.config 中,当message.Result.Success为 True 时,在我之前描述的控制器中设置 Portable 将重定向到的 defaultUrl。 You do not need to be using Forms Authentication, the mode can be set to None, but you need to have the defaultUrl for this to work.您不需要使用表单身份验证,模式可以设置为无,但您需要使用 defaultUrl 才能使其工作。

     <system.web> <authentication mode="Forms"> <forms loginUrl="~/Portable/Login" defaultUrl="~/Home/Index" /> </authentication> </system.web>

That's it!就是这样! This was a great exercise and learning experience for me.这对我来说是一次很好的锻炼和学习经历。 I am still figuring out the second part of my question where I need to send information to the Portable first (like the application title and assembly version) but I'm thinking I can do pretty much the same thing but in reverse, where I send an ICommandMessage to the Portable when my Host starts up (global.asax).我仍在弄清楚我的问题的第二部分,我需要首先将信息发送到便携式(如应用程序标题和程序集版本),但我想我可以做几乎相同的事情,但反过来,我发送当我的主机启动时向便携式设备发送 ICommandMessage (global.asax)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM