我已经使用我的WebAPI 2应用程序实现了Oauth,并且有几个应用程序访问API。经过身份验证后,在通过身份验证令牌发送请求后,我可以按如下方式访问用户:
var currentUser = RequestContext.Principal;
登录时,clientId
设置如下:
context.OwinContext.Set<AppClient>("oauth:client", client);
有没有办法访问该客户端ID?我想限制某些客户端的某些操作/控制器。有没有办法做到这一点?
我尝试按如下方式获取客户端:
var client = Request.GetOwinContext().Get<string>("oauth:client");
但这不起作用。
答案 0 :(得分:6)
登录时,您可以在GrantResourceOwnerCredentials
identity.AddClaim(new Claim("oauth:client", client));
一旦设置了用户主体的身份,就可以使用它。
您可以创建一个扩展方法来方便地提取它
public static class GenericIdentityExtensions {
const string ClientIdentifier = "oauth:client";
/// <summary>
/// Set the client id claim
/// </summary>
/// <param name="identity"></param>
/// <returns></returns>
public static bool SetClientId(this IIdentity identity, string clientId) {
if (identity != null) {
var claimsIdentity = identity as ClaimsIdentity;
if (claimsIdentity != null) {
claimsIdentity.AddClaim(new Claim(ClientIdentifier, clientId));
return true;
}
}
return false;
}
/// <summary>
/// Return the client id claim
/// </summary>
/// <param name="identity"></param>
/// <returns></returns>
public static string GetClientId(this IIdentity identity) {
if (identity != null) {
var claimsIdentity = identity as ClaimsIdentity;
if (claimsIdentity != null) {
return claimsIdentity.FindFirstOrEmpty(ClientIdentifier);
}
}
return string.Empty;
}
/// <summary>
/// Retrieves the first claim that is matched by the specified type if it exists, String.Empty otherwise.
/// </summary>
public static string FindFirstOrEmpty(this ClaimsIdentity identity, string claimType) {
var claim = identity.FindFirst(claimType);
return claim == null ? string.Empty : claim.Value;
}
}
现在,一旦拥有了用户主体,就可以从声明中提取客户端ID。
var currentUser = RequestContext.Principal;
var client = currentUser.Identity.GetClientId();
答案 1 :(得分:0)
您可以改用from selenium import webdriver
from time import sleep
from secrets import username, password
class Webot:
def __init__(self):
self.driver=webdriver.Chrome()
def login(self):
self.driver.get('https://tinder.com')
sleep(2)
fbb_btn= self.driver.find_element_by_xpath('//*[@id="content"]/div/div[1]/div/div/main/div/div[2]/div[2]/div/div/span/div[2]/button')
fbb_btn.click()
。与回答相同,在AuthenticationProperties
中,仅在发行票证时实施以下内容:
GrantResourceOwnerCredentials
然后,您将可以通过var props = new AuthenticationProperties(new Dictionary<string, string>
{
{
"client_id", clientId
},
});
var ticket = new AuthenticationTicket(identity, props);
context.Validated(ticket);