نوشته شده توسط
xamfia
سپاسگزارم دوست من.
پاسخ های مفیدی بود.
اگر امکان دارد کد را به صورت سی شارپ قرار دهید.
کد #C برای پیاده سازی احراز هویت :
using System;
using System.Net;
using System.Text;
namespace MyAuthenticationModule
{
public class MyAuthenticationModule : IAuthenticationModule
{
private string _authType = "Basic";
public Authorization Authenticate(String challenge, WebRequest request, ICredentials credentials)
{
HttpWebRequest httpWebRequest = request as HttpWebRequest;
int index = challenge.ToLower().IndexOf(_authType.ToLower());
if(-1 == index)//Basic authetication was not the challenge.
{return null;}
String domain = credentials.GetCredential(request.RequestUri, _authType).Domain;
String username = credentials.GetCredential(request.RequestUri, _authType).UserName;
String password = credentials.GetCredential(request.RequestUri, _authType).Password;
byte[] authBytes = System.Text.Encoding.ASCII.GetBytes(domain + "\\" + username + ":" + password);
String authString = Convert.ToBase64String(authBytes);
return new Authorization(_authType + " " + authString, true, "myAuth");
}
public Authorization PreAuthenticate(WebRequest request, ICredentials credentials)
{return null;}
public String AuthenticationType
{get{return _authType;}}
public bool CanPreAuthenticate
{get{return false;}}
}
}
همونطورکه می بینید در حالت Basic استفاده میشه ...
روش قدم به قدم برای پیاده سازی، پیکربندی و تست Authentication رو که از پیاده سازی IAuthenticationModule استفاده کرده؛ رو میتونید اینجا ببینید.