获取当前请求的凭证使用的WebRequest [英] Get current request's credentials for using in WebRequest
问题描述
在访问我的网站,用户必须输入自己的凭据。他们基本上是普通的目录访问凭据。
在某一点上我检查,如果他们要下载某个文件是否存在通过调用
When accessing my site, the user has to enter his credentials. They are basically plain directory access credentials. At a certain point I check if a certain file they want to download exists by calling
WebRequest req = HttpWebRequest.Create(checkUri.AbsoluteUri);
WebResponse res = req.GetResponse();
虽然我可以从浏览器访问checkUri,我得到一个401做上述检查时。我想我必须设置
Although I can access the checkUri from the browser, I get a 401 when doing the above check. I think I have to set the
req.Credentials
但我不知道在哪里当前凭据存储...
But I don't know where the current credentials are stored...
任何想法?
- 更新 -
- 集成Windows身份验证:否
- 允许匿名:关
- 分频器:链接同一网站的网页上(GET)
- 模拟:默认是关闭的(甚至不知道如何使它能够在asp.net的MVC)
推荐答案
我的网站上有类似的问题在那里我使用窗体身份验证,我能够通过使用code提供了解决这个问题的<一个href=\"http://www.$c$cproject.com/Messages/3139016/Forms-Authentication-and-using-Current-User-Creden.aspx#3139377\">here作为线程的第二个答复。
I was having a similar problem on a site where I'm using forms authentication, I was able to solve this problem by using the code provided here as the second reply in the thread.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
// Add the current authentication cookie to the request
HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
Cookie authenticationCookie = new Cookie(
FormsAuthentication.FormsCookieName,
cookie.Value,
cookie.Path,
HttpContext.Current.Request.Url.Authority);
req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(authenticationCookie);
WebResponse res = req.GetResponse();
这篇关于获取当前请求的凭证使用的WebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!