2012-07-03 11 views
13

मैं डब्ल्यूसीएफ सेवा में रोल मैनेजर का उपयोग कैसे कर सकता हूं?मैं डब्ल्यूसीएफ सेवा में रोल मैनेजर का उपयोग कैसे कर सकता हूं?

मेरे .NET एप्लिकेशन में, मैं [Authorize(Roles=)] टैग के साथ कक्षा या विधि को प्रतिबंधित कर सकता हूं। मैं इसे अपनी डब्ल्यूसीएफ सेवा के लिए कैसे सक्षम कर सकता हूं?

मैं वर्तमान में प्रत्येक समाप्ति बिंदु निम्नलिखित बाध्यकारी सेट है:

<webHttpBinding> 
    <binding name="TransportSecurity" maxReceivedMessageSize="5242880"> 
     <security mode="Transport"> 
     <transport clientCredentialType="None"/> 
     </security> 
    </binding> 
    </webHttpBinding> 

जब से मैं में उपयोगकर्ता लॉग है और प्राचार्य के साथ कुकी प्राप्त करना चाहते हैं, मैं clientCredentialType का एक और प्रकार को यह बदलने के लिए की जरूरत है ?

संपादित करें 1:

यह बाकी सोप का उपयोग कर, नहीं है। यह भी ध्यान रखना है कि यह महत्वपूर्ण है कि यह मोबाइल उपकरणों (एंड्रॉइड, आईफोन) के साथ काम करता है और सत्र बनाए रखने के लिए कुकीज़ का उपयोग कर सकता है। अब तक, मैं इस काम के प्राप्त करने में असमर्थ किया गया है, निम्नलिखित कोड/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> 

कोड

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); 
    } 

अब प्रधानाचार्य का उपयोग कर अनुमति, मुझे SecurityException मिलती है (मुझे पता है कि भूमिका सर्वर पर मान्य है)

[PrincipalPermission(SecurityAction.Demand, Role = Constants.RoleUser)] 
    public Message TestRoles() 
    { 
     var context = NetworkHelper.GetWebOperationContext(); 

     return context.CreateTextResponse("You have successfully activated the endpoint."); 
    } 

क्या मुझे यहां एक महत्वपूर्ण कदम याद आ रहा है?

उत्तर

9

मैं के बारे में लिखा था a blog post कैसे WCF साथ ASP.NET प्रमाणीकरण का उपयोग करने के लिए; यह का सार है कि आप बाध्यकारी निम्नलिखित का उपयोग करना चाहते है:

<basicHttpBinding> 
    <binding> 
     <security mode="TransportWithMessageCredential"> 
     <message clientCredentialType="UserName"/> 
     </security> 
    </binding> 
    </basicHttpBinding> 

आप निम्न serviceBehavior

<behavior> 
     <!-- no need for http get; 
      but https get exposes endpoint over SSL/TLS--> 
     <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"/> 
     <!-- the authorization and credentials elements tie 
     this behavior (defined as the default behavior) to 
     the ASP.NET membership framework--> 
     <serviceAuthorization 
      principalPermissionMode="UseAspNetRoles" 
      roleProviderName="AspNetRoleProvider" /> 
     <serviceCredentials> 
     <userNameAuthentication 
      userNamePasswordValidationMode="MembershipProvider" 
      membershipProviderName="AspNetMembershipProvider" /> 
     </serviceCredentials> 
    </behavior> 

एक महत्वपूर्ण नोट करने के लिए बिंदु को लागू करना चाहिए कि आप SSL अगर आप का उपयोग करना चाहिए है एक नाम और पासवर्ड के साथ डब्ल्यूसीएफ सुरक्षित करने जा रहा है, यही कारण है कि परिवहन सुरक्षा निर्दिष्ट है।

एक बार ऐसा करने के बाद, आप अपनी सेवा विधियों को सुरक्षित करने के लिए PrincipalPermission विशेषता का उपयोग करने में सक्षम होना चाहिए।

+0

मुझे लगता है, मैंने अभी देखा है कि 'प्राधिकरण' विशेषता एमवीसी केंद्रित है। मुझे 'प्रिंसिपल प्रिमिशन' का उपयोग करना होगा, मुझे लगता है कि यह काम एक समान फैशन में है? – Cody

+0

+1: बढ़िया जवाब! स्पष्ट रूप से माइक्रोसॉफ्ट ने डब्ल्यूसीएफ के साथ भूमिका प्रदाताओं का उपयोग करने के बारे में एमएसडीएन पर एक लेख के साथ इस विषय का पालन किया है: http://msdn.microsoft.com/en-us/library/aa702542.aspx – reSPAWNed

+0

हाय। क्या आप इस प्रश्न पर एक नज़र डालें : https: //stackoverflow.com/questions/45770217/my-customauthorizationpolicy-evaluate-method-never-fires –

2

मुझे लगता है कि कुछ इस तरह आप जो खोज रहे हैं प्रदान करेगा: MSDN Article:

// Only members of the SpecialClients group can call this method. 
[PrincipalPermission(SecurityAction.Demand, Role = "SpecialClients")] 
public void DoSomething() 
{ 
} 

आप यहाँ आपकी सेवा स्थापित करने के लिए विभिन्न विकल्पों पर एक अच्छा लेख पा सकते हैं। आप की तरह कुछ के साथ अपने कॉन्फ़िगरेशन को अपडेट कर सकते हैं:

<security mode="TransportWithMessageCredential" > 
    <transport clientCredentialType="Windows" /> 
</security> 
2

दो साल पहले मैंने फॉर्म प्रमाणीकरण और मुख्य अनुमति विशेषताओं के साथ एक डब्ल्यूसीएफ एकीकरण पर ब्लॉग किया है।मेरा मानना ​​है कि आप इस उपयोगी लगेगी:

http://netpl.blogspot.com/2010/04/aspnet-forms-authentication-sharing-for.html

+0

यह सुनिश्चित नहीं है कि यह मेरी स्थिति में मदद करता है। – Cody

+0

आप मेरे दृष्टिकोण की तुलना में क्या चूक जाते हैं: 1) आप threadprincipal को context.user 2 पर सेट नहीं करते हैं) आपके पास एएसपीनेट संगतता नहीं है 3) आपको अपने प्रिंसिपल पैरामिशन में प्रमाणीकृत = सत्य को भी रिमांड करना चाहिए विशिष्ट भूमिका –

+0

मेरे पास इन सभी चीजों को सेट किया गया है, और यह काम नहीं करता है। – Cody

1

जांच इस सवाल Passing FormsAuthentication cookie to a WCF service। मुख्य बिंदु यह है कि कुकी डोमेन/एएसपी सेटिंग्स/एथ एन्क्रिप्शन कुंजी के कारण प्रमाणीकरण कुकी या तो भेज या प्राप्त नहीं की जा सकती है। मैं आपको डिबगिंग उद्देश्यों के लिए उस कुकी मान को डंप करने की सलाह देता हूं। अनुरोध। कुकीज ["। ASPXAUTH"]।

3

मुझे बहुत समय पहले प्रिंसिपल के साथ भी यही समस्या थी। मुझे विवरण याद नहीं है, लेकिन मेरी पुरानी परियोजना से, इसे आजमाएं:

i। आप 2 सहायक कक्षाएं जोड़ने के लिए:


using System.Web; 
using System.IdentityModel.Claims; 
using System.IdentityModel.Policy; 

namespace TicketingCore 
{ 
    public class HttpContextPrincipalPolicy : IAuthorizationPolicy 
    { 
     public bool Evaluate(EvaluationContext evaluationContext, ref object state) 
     { 
      HttpContext context = HttpContext.Current; 

      if (context != null) 
      { 
       evaluationContext.Properties["Principal"] = context.User; 
      } 

      return true; 
     } 

     public System.IdentityModel.Claims.ClaimSet Issuer 
     { 
      get { return ClaimSet.System; } 
     } 

     public string Id 
     { 
      get { return "TicketingCore HttpContextPrincipalPolicy"; } 
     } 
    } 
} 


using System; 
using System.Collections.Generic; 
using System.IdentityModel.Claims; 
using System.IdentityModel.Policy; 
using System.Text; 
using System.Web; 
using System.Security.Principal; 

namespace TicketingCore 
{ 
    // syncs ServiceSecurityContext.PrimaryIdentity in WCF with whatever is set 
    // by the HTTP pipeline on Context.User.Identity (optional) 
    public class HttpContextIdentityPolicy : IAuthorizationPolicy 
    { 
     public bool Evaluate(EvaluationContext evaluationContext, ref object state) 
     { 
      HttpContext context = HttpContext.Current; 

      if (context != null) 
      { 
       // set the identity (for PrimaryIdentity) 
       evaluationContext.Properties["Identities"] = 
        new List<IIdentity>() { context.User.Identity }; 

       // add a claim set containing the client name 
       Claim name = Claim.CreateNameClaim(context.User.Identity.Name); 
       ClaimSet set = new DefaultClaimSet(name); 
       evaluationContext.AddClaimSet(this, set); 
      } 

      return true; 
     } 

     public System.IdentityModel.Claims.ClaimSet Issuer 
     { 
      get { return ClaimSet.System; } 
     } 

     public string Id 
     { 
      get { return "TicketingCore HttpContextIdentityPolicy"; } 
     } 
    } 
} 

ii। इन 2 नीति जोड़ने के लिए webconfig बदलें, यहां मेरे config है (प्रारूप: policyType = "namespace.class, विधानसभा" जोड़)

<serviceBehaviors> 
    <behavior name="ServiceBehavior"> 
     <serviceCredentials> 
     <userNameAuthentication userNamePasswordValidationMode="MembershipProvider" membershipProviderName="SQLMembershipProvider"/> 
     </serviceCredentials> 
     <serviceAuthorization principalPermissionMode="Custom"> 
     <authorizationPolicies> 
      <add policyType="TicketingCore.HttpContextIdentityPolicy, TicketingCore"/> 
      <add policyType="TicketingCore.HttpContextPrincipalPolicy, TicketingCore"/> 
     </authorizationPolicies> 
     </serviceAuthorization> 
     <serviceMetadata httpGetEnabled="true"/> 
     <serviceDebug includeExceptionDetailInFaults="false"/> 
    </behavior> 
    </serviceBehaviors> 

iii। सुनिश्चित करें कि कुकी और भूमिका ठीक काम कर रही है

नोट: मुझे समाधान के स्रोत को भी याद नहीं है, आपको यह जानने के लिए कक्षा के नाम पर Google की आवश्यकता हो सकती है, उम्मीद है कि यह आपके लिए उपयोगी होगा, शुभकामनाएँ!

+0

यह इंटरनेट पर सबसे पूरा उदाहरण है और डब्ल्यूसीएफ सेवा में फॉर्म प्रमाणीकरण का उपयोग करके प्राधिकरण करने का सबसे सही तरीका प्रतीत होता है। एक संबंधित समस्या यह है कि सेवा आमंत्रण के भीतर प्रमाणीकरण कैसे करें और आईआईएस वेब.कॉन्फिग पर भरोसा करने के बजाय 401 त्रुटि लौटाएं ताकि अज्ञात उपयोगकर्ताओं को सेवा उपनिर्देशिका तक पहुंच से इनकार कर दिया जा सके। – Monstieur

+0

नमूना परियोजना कहीं भी? – Rhyous

संबंधित मुद्दे

 संबंधित मुद्दे