如何从控制台应用程序的ASP.NET MVC应用程序认证 [英] How to authenticate in an ASP.NET MVC application from a console application

查看:174
本文介绍了如何从控制台应用程序的ASP.NET MVC应用程序认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个发送XML到一个MVC应用程序,并收到另一个XML作为响应的控制台应用程序。它完美的,但我想补充的授权(原因很明显)。

I have a console application that sends a XML to a MVC application and receive another XML as a response. It works perfectly, but I want to add authorization (for obvious reasons).

下面是从控制台应用程序中的code:

Here is the code from the console application:

using (var wc = new WebClient())
{
    return GetXmlFromBytes(
        wc.UploadData("URL", GetBytesFromXml(xmlToSend))
    );
}

这是从MVC应用程序中的code:

And here is the code from the MVC application:

public ActionResult DoSomething()
{
    XElement xml = XElement.Load(new System.IO.StreamReader(Request.InputStream));
    var response = InsertDataFromXml(xml);
    return File(GenerateFileFromResponse, "text/xml", "result.xml");
}

和它的作品。因此,要实现授权,我增加了以下code:

And it works. So, to implement the authorization, I added the following code:

控制台(加入 wc.Credentials

using (var wc = new WebClient())
{
    wc.Credentials = new NetworkCredential("user", "password");
    return GetXmlFromBytes(
        wc.UploadData("URL", GetBytesFromXml(xmlToSend))
    );
}

(添加了 [授权]

MVC应用程序:

MVC application (added the [Authorize]):

[Authorize]
public ActionResult DoSomething()
{
    XElement xml = XElement.Load(new System.IO.StreamReader(Request.InputStream));
    var response = InsertDataFromXml(xml);
    return File(GenerateFileFromResponse, "text/xml", "result.xml");
}

和它不工作。我不知道是否需要这些信息来解决这个问题,但我的的web.config 具有以下项目:

And it doesn't work. I don't know if this information is needed to solve this, but my web.config has the following item:

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication> 

事实上,该MVC应用程序发回的文件是登录页面的HTML!

In fact, the file that the MVC application sends back is the HTML of the LogOn page!

什么是我必须做的来解决这个?在任何参数缺少 NetworkCredentials ?我知道这可以用被实例化,但我不知道什么是在MVC应用程序的用户的域。

What do I have to do to solve this? Is any parameter missing in the NetworkCredentials? I know it can be instantiated with a domain, but I don't know what is the domain of the users in the MVC application.

和,只是为了确保我放心用户和密码是有效的。

And, just to make sure: I assured "user" and "password" are valid.

推荐答案

您正在混合凭据类型;

You're mixing credential types;

wc.Credentials = new NetworkCredential("user", "password");

是HTTP验证。

is for HTTP authentication.

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication> 

是窗体身份验证。

is forms authentication.

这是完全不同的,不兼容。在命令行应用程序使用窗体身份验证是具有挑战性的,你需要去到登录页面的请求,POST用户名和密码,然后取身份验证cookie是回报,并附加在随后的请求。

These are entirely different, and not compatible. Using forms authentication from a command line app is challenging, you'd need to go to the login page with a request, POST the username and password, then take the authentication cookie that is return and attach it to subsequent requests.

这篇关于如何从控制台应用程序的ASP.NET MVC应用程序认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆