Tag: 认证

如何使ServiceStack身份validation工作? (使用iPhone客户端)

我们聘请了一位为我们编写iPhone应用程序的承包商,我开始用ServiceStack为它编写后端服务。 我一般都在努力获得授权:使用何种授权以及如何实施授权。 我对ServiceStack,HTTP和授权(我)了解不多。我读过这篇文章 ,但我可能还是做错了。 我将使用现有遗留数据库中的用户名和密码进行身份validation(但没有别的 – 没有注册新用户,没有不同的权限。只是进行身份validation)。 所以我需要编写自己的提供者。 我已经设法在本教程的帮助下实现了一个有效的CredentialsAuthProvider 。 它在我在浏览器中测试时有效: 致电我的服务并获得401 POST到auth/credentials 再次呼叫我的服务并获得正确的结果 但是,我注意到当我在Fiddler中尝试时,相同的工作流程不起作用。 POST到auth/credentials有效。 我发布这个: POST http://localhost:52690/auth/credentials?format=json HTTP/1.1 User-Agent: Fiddler Host: localhost:52690 Content-Length: 74 Content-Type: application/json; charset=utf-8 { “UserName”: “chspe”, “Password”: “xyz”, “RememberMe”: true } ……得到这个: HTTP/1.1 200 OK Server: ASP.NET Development Server/10.0.0.0 Date: Fri, 14 Jun 2013 13:07:35 GMT X-AspNet-Version: 4.0.30319 X-Powered-By: […]

在C#中validationGoogle ID令牌

我需要在ASP.NET web api上validation从移动设备传递的Google ID令牌。 Google在这里有一些示例代码,但它依赖的是JWT NuGet包,它只是.Net 4.5(我使用的是C#/ .Net 4.0)。 有没有人知道没有这些包装的任何样品或自己实现了这个? 使用该软件包使得很难找到没有它我需要做的事情。

使Web API身份validation返回401而不是重定向到登录页面

我在Web MVC中使用带有OWIN身份validation的Web API。 我在Web.Config中使用为我的Web MVC,所以它重定向到登录页面。 我正在使用[System.Web.Http.Authorize]属性来授权我的Web API。 但不知何故,由于上述配置,API重定向到登录页面,就像我的MVC应用程序一样。 我想要做的是保持重定向Web MVC的function,但返回401 for Web API。 我怎样才能做到这一点? 我应该为Web API创建自定义授权属性吗? – 编辑 – 我发现WebApi.Owin中这篇文章中的SuppressDefaultHostAuthentication的答案也抑制了webapi之外的身份validation 所以我只需在Startup.cs添加几行代码。 我的所有控制器都配置了“api”前缀路由。 HttpConfiguration config = new HttpConfiguration(); //..some OWIN configuration app.Map(“/api”, inner => { inner.UseWebApi(config); }); 确保在Web Api配置行之后放置app.Map() 。 否则,它将给MVC应用程序带来错误。

如何从控制台应用程序在ASP.NET MVC应用程序中进行身份validation

我有一个控制台应用程序,它将XML发送到MVC应用程序并接收另一个XML作为响应。 它工作得很好,但我想添加授权(出于显而易见的原因)。 以下是控制台应用程序的代码: using (var wc = new WebClient()) { return GetXmlFromBytes( wc.UploadData(“URL”, GetBytesFromXml(xmlToSend)) ); } 这是来自MVC应用程序的代码: public ActionResult DoSomething() { XElement xml = XElement.Load(new System.IO.StreamReader(Request.InputStream)); var response = InsertDataFromXml(xml); return File(GenerateFileFromResponse, “text/xml”, “result.xml”); } 它有效。 因此,为了实现授权,我添加了以下代码: 控制台(添加了wc.Credentials ): using (var wc = new WebClient()) { wc.Credentials = new NetworkCredential(“user”, “password”); return GetXmlFromBytes( wc.UploadData(“URL”, GetBytesFromXml(xmlToSend)) […]

如何使用Asp.Net Identity 2将用户添加到角色后使.AspNet.ApplicationCookie失效?

我有两个与此相关的问题: 1)我需要使用Asp.Net Identity 2添加/删除一些远程用户到Role后无效.AspNet.ApplicationCookie我尝试使用UpdateSecurityStamp,但由于没有更改密码或用户名,因此SecurityStamp保持不变。 当我使用ApplicationRoleManger时,我可以看到用户角色已更新,但在User.Identity声明中,它们保持不变。 2).AspNet.ApplicationCookievalidation如何工作以及如何访问它? 我试图使用此代码,但没有任何效果 什么是ASP.NET Identity的IUserSecurityStampStore 接口? 更新:这是我的Cookievalidation设置: app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString(“/Account/Login”), Provider = new CookieAuthenticationProvider { OnValidateIdentity = SecurityStampValidator.OnValidateIdentity( validateInterval: TimeSpan.FromSeconds(0), regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)), OnApplyRedirect = ctx => { if (!IsApiRequest(ctx.Request)) { ctx.Response.Redirect(ctx.RedirectUri); } } } }); 我可以看到user.GenerateUserIdentityAsync(manager)仅在登录时被命中。

ADAL .Net Core nuget包不支持UserPasswordCredential

在ADAL.Net 3.x中,UserPasswordCredential在2.x的UserCredential之上引入。 但同一个UserPasswordCredential在同一个nuget包下的.Net Core中没有公开? UserCredential类只有一个属性UserName namespace Microsoft.IdentityModel.Clients.ActiveDirectory { // // Summary: // Credential used for integrated authentication on domain-joined machines. public class UserCredential { // // Summary: // Constructor to create user credential. Using this constructor would imply integrated // authentication with logged in user and it can only be used in domain joined scenarios. […]

Azure移动服务,HttpClient,授权

我可以使用.NET的HttpClient来访问Azure Mobile服务吗? 如何使用HttpClient对移动服务自带的自定义身份validation/授权模式进行身份validation ? 这总是返回401,因为我没有传递任何身份validation凭据: var client = new HttpClient(); var response = client.GetAsync(“http://localhost:49190/api/test”).Result; 此外,为什么我使用移动服务客户端 ,为什么我的应用程序密钥,主密钥或用户身份validation密钥总是返回(401)未授权? 客户: var mobileClient = new MobileServiceClient(“http://localhost:49190/”, “[my key]”); var response = mobileClient.InvokeApiAsync(“test”).Result; 服务方面: [AuthorizeLevel(AuthorizationLevel.Application)] public class TestController : ApiController { public ApiServices Services { get; set; } // GET api/Test public string Get() { Services.Log.Info(“Hello from custom controller!”); return […]

如何将OnAuthenticated事件中的信息传递给控制器​​和SignIn?

正如我在这里发现的那样 ,我无法打电话 HttpContext.GetOwinContext().Authentication.SignIn(…) 在FacebookAuthenticationProvider OnAuthenticated事件。 这似乎是一个不同的背景。 我的用户将来自Facebook,这意味着我将需要检索id , email等数据……这些信息可以通过OnAuthenticated事件访问,我需要这些信息才能登录。 我需要在我的控制器上访问这些信息…… 我在活动中试过这个: context.OwinContext.Set(“FacebookUser”, rawUserObjectFromFacebookAsJson); 但是在控制器上如果我尝试检索它,它就是null。 var fbuser = HttpContext.GetOwinContext() .Get(“FacebookUser”); 所以我的问题是,如何将这些数据传递给控制器​​,以便我可以登录?

ASP.NET Core 2.0将Cookie和承载授权结合在一起,用于同一端点

我在VS17中使用“Web应用程序(模型 – 视图 – 控制器)”模板和“.Net Framework”+“ASP.NET Core 2”作为配置创建了一个新的ASP.NET核心Web应用程序项目。 身份validation配置设置为“个人用户帐户”。 我有以下示例端点: [Produces(“application/json”)] [Route(“api/price”)] [Authorize(Roles = “PriceViwer”, AuthenticationSchemes = “Cookies,Bearer”)] public class PriceController : Controller { public IActionResult Get() { return Ok(new Dictionary { {“Galleon/Pound”, “999.999” } ); } } “Cookies,Bearer”是通过连接CookieAuthenticationDefaults.AuthenticationScheme和JwtBearerDefaults.AuthenticationScheme 。 目标是能够为端点配置授权,以便可以使用令牌和cookie身份validation方法访问它。 以下是我在Startup.cs中进行身份validation的设置: services.AddAuthentication() .AddCookie(cfg => { cfg.SlidingExpiration = true;}) .AddJwtBearer(cfg => { cfg.RequireHttpsMetadata = false; […]

使用ServiceStack使用REST Web服务时的用户身份validation

ServiceStack文档中有很多关于如何使用服务器端实现用户身份validation的示例。 但是如何在客户端设置用户凭据? 我使用ServiceStack来使用这样的JSON REST服务: var restClient = new JsonServiceClient (baseUri); var response = restClient.Get (“/some/service”); 如何在请求中添加任何forms的身份validation? 我想要使​​用的Web服务使用OAuth 1.0 ,但我也有兴趣添加自定义身份validation。 在我的代码中,我之前已经成功执行了OAuth令牌交换,因此我已经拥有了一个有效的访问令牌,现在需要使用此访问令牌及其token_secret对每个REST请求进行token_secret 。