gimana car bikin basic auth di server IIS untuk REST WCF service?
udah saya coba pake ini, tapi masukin user : pas nya ga mau masuk... ada yang punya cara lain?
Spoiler for "intercept basic auth" :
Code:
<serviceBehaviors>
<behavior name="RestEmpSvcBehavior">
<serviceDebug />
<serviceMetadata httpGetEnabled="true" />
<serviceAuthorization serviceAuthorizationManagerType="RestEmp.Security, RestEmp" />
</behavior>
</serviceBehaviors>
Code:
public class Security : ServiceAuthorizationManager
{
private const string UserName = "123";
private const string Password = "123";
protected override bool CheckAccessCore(OperationContext operationContext)
{
string authHeader = WebOperationContext.Current.IncomingRequest.Headers["Authorization"];
if ((authHeader != null) && (authHeader != string.Empty))
{
string[] svcCredentials = System.Text.ASCIIEncoding.ASCII
.GetString(Convert.FromBase64String(authHeader.Substring(6)))
.Split(':');
var user = new { Name = svcCredentials[0], Password = svcCredentials[1] };
if ((user.Name.Equals(UserName) && user.Password.Equals(Password)))
return true;
else
return false;
}
else
{
WebOperationContext.Current.OutgoingResponse.Headers.Add("WWW-Authenticate: Basic realm="PsmProvider"");
throw new WebFaultException(HttpStatusCode.Unauthorized);
}
}
}
}