Come posso utilizzare il gestore ruoli in un servizio WCF?Come posso utilizzare il gestore ruoli in un servizio WCF?
Nella mia applicazione .NET, posso limitare una classe o un metodo con il tag [Authorize(Roles=)]
. Come posso abilitare questo per il mio servizio WCF?
ho attualmente la seguente serie vincolante per ciascun endpoint:
<webHttpBinding>
<binding name="TransportSecurity" maxReceivedMessageSize="5242880">
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
Dato che voglio avere il registro utente e di ricevere un cookie con il preside, ho bisogno di cambiare questo ad un altro tipo di clientCredentialType
?
Edit 1:
Questo è utilizzando REST, SOAP non. È anche importante notare che è importante che funzioni con i dispositivi mobili (Android, iPhone) e che possa utilizzare i cookie per mantenere una sessione. Finora, sono stato in grado di ottenere questo lavoro, utilizzando il seguente codice/config: File
Config:
<roleManager enabled="true" defaultProvider="ActiveDirectoryRoleProvider" cacheRolesInCookie="true" cookieName="RoleCookie" cookiePath="/" cookieTimeout="30" cookieRequireSSL="false" cookieSlidingExpiration="true" createPersistentCookie="false" cookieProtection="All">
<providers>
<clear />
<add name="ActiveDirectoryRoleProvider" connectionStringName="ADServices" connectionUsername="" connectionPassword="" attributeMapUsername="sAMAccountName" type="" />
</providers>
</roleManager>
<membership defaultProvider="MembershipADProvider">
<providers>
<add name="MembershipADProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" applicationName="" connectionStringName="ADServices" connectionUsername="" connectionPassword="" attributeMapUsername="sAMAccountName" />
</providers>
</membership>
<bindings>
<webHttpBinding> <!-- webHttpBinding is for REST -->
<binding name="TransportSecurity" maxReceivedMessageSize="5242880">
<security mode="Transport">
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
<serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="ActiveDirectoryRoleProvider" />
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="MembershipProvider" membershipProviderName="MembershipADProvider" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
Codice
public void SignIn2(string userName, bool createPersistentCookie)
{
if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
// put the attributes in a string for userdata
string userData = "";
// create the ticket
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
userName,
DateTime.Now,
DateTime.Now.AddMinutes(240),
createPersistentCookie,
userData);
// Now encrypt the ticket.
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
// Create a cookie and add the encrypted ticket to the cookie as data.
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
// add the cookie
HttpContext.Current.Response.Cookies.Add(authCookie);
}
Ora utilizzando il Principal Autorizzazione, ottengo un SecurityException
(so che il ruolo è valido sul server)
[PrincipalPermission(SecurityAction.Demand, Role = Constants.RoleUser)]
public Message TestRoles()
{
var context = NetworkHelper.GetWebOperationContext();
return context.CreateTextResponse("You have successfully activated the endpoint.");
}
Mi manca un passaggio cruciale qui?
Vedo, ho appena visto che l'attributo 'Autorizza' è MVC-centrico. Dovrò usare il 'PrincipalPermission', presumo che funzioni in modo molto simile? – Cody
+1: ottima risposta! Apparentemente Microsoft ha seguito questo argomento con un articolo su MSDN sull'utilizzo dei provider di ruolo con WCF: http://msdn.microsoft.com/en-us/library/aa702542.aspx – reSPAWNed
Ciao. Per favore, dai un'occhiata a questa domanda : https: //stackoverflow.com/questions/45770217/my-customauthorizationpolicy-evaluate-method-never-fires –