Tag: basic authentication

如何在IAuthenticationFilter实现中设置WWW-Authentication标头?

我正在使用MVC5的IAuthenticationFilter接口实现基本身份validation。 我的理解是,现在这是首选方法,而不是使用DelegatingHandler。 我已经开始工作,但响应中没有返回www-authenticate标头。 这是我对ChallengeAsync的实现: public async Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken) { var result = await context.Result.ExecuteAsync(cancellationToken); if (result.StatusCode == HttpStatusCode.Unauthorized) { result.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue(“Basic”, “realm=localhost”)); } } 如果我在AuthenticateAsync中设置它,则会返回标头,但我认为我应该在ChallengeAsync中设置它。 示例实现很难找到。

为什么使用“新的NetworkCredential(用户名,密码)”不适用于我的网站的基本身份validation(来自WinForms C#应用程序)?

我有一个使用基本身份validation(用户名/密码)的网站。 为什么以下代码不起作用? 当我运行它时,Web应用程序将我带到登录控制器,而我期望它应该已经过身份validation,因为我正在填充凭据。 换句话说,我正在尝试确认如何在.NET中确认我的winforms HttpWebRequest,以便它将自动化身份validation过程。 我假设NetworkCredential是应该这样做的.net类? 或者在.NET中是否期望有一些手动两步过程你必须自己实现? 这是代码: // Create a new webrequest to the mentioned URL. var uri = new Uri(“http://10.1.1.102:3000/download/sunset”); var myWebRequest = (HttpWebRequest)WebRequest.Create(uri); myWebRequest.PreAuthenticate=true; var networkCredential=new NetworkCredential(“test”, “asdfasdf”); myWebRequest.Credentials=networkCredential; var myWebResponse = (HttpWebResponse)myWebRequest.GetResponse(); Console.Out.WriteLine(“STATUS = ” + myWebResponse.StatusCode); var stream = myWebResponse.GetResponseStream(); var reader = new StreamReader(stream); string text_read = reader.ReadToEnd(); Console.WriteLine(text_read); […]

默认情况下发送BASIC auth,而不是等待HTTP 401

我有一个Web服务,要求BASIC身份validation标头出现在请求中,否则服务将返回HTTP 401(未经授权)。 这样做 – 当挑战回来时,浏览器(在这种情况下,Chrome)弹出并要求提供凭据。 然后保存它们以备将来请求。 我的问题是,现在对服务的每个后续请求都有两个请求 – 一个没有auth(接收401),然后浏览器立即在头中回复正确的auth。 有没有办法强制浏览器(可能通过一个特殊的标题)提供凭据,而不必每次都被Web服务明确询问?

.NET POST到PHP页面

我正在尝试从我的C#.NET应用程序实现一个非常基本的系统,该系统将我的Web服务器上的机器的IP地址发送到authenticate.php。 php页面将针对数据库检查此IP地址,并以“是”或“否”回复。 自从我使用PHP以来已经很长时间了,我有点困惑。 这是我的.NET函数的样子。 public static bool IsAuthenticated() { string sData = getPublicIP(); Uri uri = new Uri(“http://www.mysite.com/authenticate.php”); if (uri.Scheme == Uri.UriSchemeHttp) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Method = WebRequestMethods.Http.Post; request.ContentLength = sData.Length; request.ContentType = “application/x-www-form-urlencoded”; // POST the data to the authentication page StreamWriter writer = new StreamWriter(request.GetRequestStream()); writer.Write(sData); writer.Close(); // Retrieve response from […]

通过Angular进行基本身份validation,为什么抛出错误401

我有一个Angular应用程序,我正在尝试使用基本身份validation对我的REST服务进行身份validation。 我正在添加授权标题以及在base64中编码的相应“Base {username:password}”,我正在调用我的rest api但是仍然会回到401.我显然在这里错过了一步…… 这是角度代码: angular .module(‘myApp.services’) .factory(‘AuthenticationService’, [‘$http’, ‘$q’, ‘$location’, ‘Base64’, ‘SessionService’, function ($http, $q, $location, encoder, session) { return { authenticate: function (user, password) { var deferred = $q.defer(); var url = “http://localhost:28924/api/login”; if (user && password) { var encoded = encoder.encode(user + ‘:’ + password); $http.defaults.headers.common.Authorization = ‘Basic ‘ + encoded; console.log(‘here’); […]

为什么找不到我的BasicAuthenticationModule?

我正在尝试创建自己的基本身份validation实现。 我将BasicAuthenticationModule.cs存储在我的solution\Modules ,其名称空间为: namespace Web_API.Modules { public class BasicAuthenticationModule2 : IHttpModule 我已将它添加到我的web.config中: 运行这个让我: HTTP Error 500.19 – Internal Server Error – Cannot add duplicate collection entry of type ‘add’ with unique key attribute ‘name’ set to ‘BasicAuthenticationModule’ 有人有线索吗?

在重定向时保持HTTP Basic Authentification存活

我们正在使用基本身份validation的Web服务。 这一切都很好,直到Web服务的所有者实现了平衡服务。 这只是将请求重定向到不同的Web服务实例。 问题是重定向后基本身份validation失败。 有“请求身份validation凭据未通过”exception。 附加信息: 我们必须手动创建请求。 var req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(Settings.Default.HpsmServiceAddress)); req.Headers.Add(“Authorization”, “Basic aaaaaaaaaaa”); req.PreAuthenticate = true; req.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested; req.UserAgent = “Apache-HttpClient/4.1.1 (java 1.5)”; req.KeepAlive = false; ServicePointManager.Expect100Continue = false; req.ContentType = “text/xml; charset=utf-8”; req.Method = “POST”; req.Accept = “gzip,deflate”; req.Headers.Add(“SOAPAction”, actionName); byte[] buffer = Encoding.UTF8.GetBytes(envelop); Stream stm = req.GetRequestStream(); stm.Write(buffer, 0, buffer.Length); […]

通过http发送基本身份validation

我试图从需要基本身份validation的页面中读取源代码。 但是,在我的HttpWebRequest中使用Header甚至Credentials,我仍然会收到未经授权的exception[401]。 string urlAddress = URL; string UserName = “MyUser”; string Password = “MyPassword”; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress); if (UserName != string.Empty) { string encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding(“ISO-8859-1”).GetBytes(UserName + “:” + Password)); request.Headers.Add(“Authorization”, “Basic ” + encoded); System.Net.CredentialCache credentialCache = new System.Net.CredentialCache(); credentialCache.Add( new System.Uri(urlAddress), “Basic”, new System.Net.NetworkCredential(UserName, Password) ); request.Credentials = credentialCache; } HttpWebResponse response […]

使用wsHttpBinding的WCF服务 – 操作HTTP请求标头

我一直在关注本教程,以便在我的WCF服务中使用传输安全性进行用户名身份validation。 然而,教程指的是使用basicHttpBinding这是不可接受的 – 我需要wsHttpBinding 。 我们的想法是在WCF服务上有一个自定义的BasicAuthenticationModule ,它将从HTTP请求中读取“Authorization”头,并根据“Authorization”头内容执行auth过程。 问题是缺少“授权”标题! 我已经通过自定义行为实现了IClientMessageInspector ,以便操作传出消息并添加自定义SOAP标头。 我在BeforeSendRequest函数中添加了以下代码: HttpRequestMessageProperty httpRequest = request.Properties.Where(x => x.Key == “httpRequest”).Single().Value; httpRequest.Headers.Add(“CustomHeader”, “CustomValue”); 这应该工作,并根据许多Web资源,它适用于basicHttpBinding但不适用于wsHttpBinding 。 当我说“工作”时,我的意思是WCF服务成功接收了标头。 这是在WCF服务端检查收到的HTTP消息的简化函数: public void OnAuthenticateRequest(object source, EventArgs eventArgs) { HttpApplication app = (HttpApplication)source; //the Authorization header is checked if present string authHeader = app.Request.Headers[“Authorization”]; if (string.IsNullOrEmpty(authHeader)) { app.Response.StatusCode = 401; app.Response.End(); } […]

Web Api不会使用jQuery Ajax和Basic Auth下载文件

我正在使用ASP.NET Web API构建Web服务(和站点)的原型,该原型具有下载文件的方法。 当前端的用户按下导出按钮时,控制器发出并接收jQuery ajax GET请求,然后控制器调用名为Excel的方法(如下所示)。 该方法运行没有任何问题并完成。 当我在标题中查看Chrome时(请参阅https://skydrive.live.com/redir?resid=2D85E5C937AC2BF9!77093 ),它会收到响应(据我所知)所有正确的标题。 我正在使用Basic Auth,因此使用http授权头传输用户凭据,我已使用Ajax选项手动将其添加到每个jQuery Ajax请求中。 var excelRequest = $.ajax({ url: ‘http://localhost:59390/api/mycontroller/excel’, cache: false, type: ‘GET’, data: gridString, dataType: ‘json’, contentType: ‘application/json; charset=utf-8’ }); $.ajaxSetup({ beforeSend: function (xhr) { SetAuthRequestHeader(xhr) } }); function SetAuthRequestHeader(jqXHR) { var usr = “Gebruiker2”; // TODO: Change, this is for testing only. var pw […]